Home >Backend Development >PHP Problem >How to convert json to object array in php
How to convert json into an object array in php?
In PHP, you can use the json_decode function to convert json into an object array.
json_decode is a PHP built-in function added after php5.2.0. Its function is to encode strings in JSON format.
So how to use this function?
The syntax rules of json_decode:
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
json_decode accepts a JSON format string and converts it into a PHP variable. When the parameter $assoc is TRUE, array will be returned, otherwise object will be returned. .
JSON format string
$json = '{"a":"php","b":"mysql","c":3}';
where a is the key, and php is the key value of a.
Example:
Program output:
stdClass Object ( [a] => php [b] => mysql [c] => 3 ) Array ( [a] => php [b] => mysql [c] => 3 )
Under the premise of the above code
Access the value of a of the object type $json_Class
echo $json_Class->{'a'};
Program output: php
Access the value of a in the array type $json_Array
echo $json_Array['a'];
Program output: php
Recommended: "php tutorial 》
The above is the detailed content of How to convert json to object array in php. For more information, please follow other related articles on the PHP Chinese website!