Home > Article > Backend Development > How to use php json_encode
In current interface development, there are very few xml formats, and most of them have been replaced by json. PHP, which is known as a language specially developed for interfaces, naturally has its powerful characteristics. That is its built-in function json_encode.
The function prototype is function json_encode($value,$option=0)
where $value is the data we want to convert to json. It can be said that the array and object. (The requirement is utf8 encoding)
$option is the conversion parameter. A binary mask composed of the following constants:
JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, among which we often use the JSON_FORCE_OBJECT parameter, which is to force the data to be converted to j son object .
Let’s take an example below
$arr = ['name'=>'梦回故里','age'=>'18']; die(json_encode($arr));
The execution result is:
class A{ public $name = ""; public $age = ""; } $a = new A(); $a->name = "梦回故里"; $a->age= "18"; die(json_encode($a));
This is the result of my operation.
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to use php json_encode. For more information, please follow other related articles on the PHP Chinese website!