In PHP, you can use json_decode() to convert json data into an array or object type, the syntax is "json_decode($json,$assoc)"; when the parameter "$assoc" is omitted, json will be converted into Object, when the value of this parameter is set to "TRUE", json will be converted into an array.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the json_decode() function Decodes JSON data and converts JSON data into the appropriate PHP data type (object or array).
Conversion syntax:
json_decode($json,$assoc)
$assoc parameter can be omitted, the default value is false;
If this parameter is omitted, the JSON data will be converted into an object
If the value is set to TRUE, it will be converted to an array.
Example 1: Convert json value to object
<?php header("Content-type:text/html;charset=utf-8"); $json = '{"a":"php","b":"mysql","c":3}'; var_dump($json); $obj=json_decode($json); var_dump($obj); ?>
Example 2: Convert json value to array
<?php header("Content-type:text/html;charset=utf-8"); $json = '{"a":"php","b":"mysql","c":3}'; var_dump($json); $arr=json_decode($json,TRUE); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert json value to array or object in php. For more information, please follow other related articles on the PHP Chinese website!