Home > Article > Backend Development > How to convert json php object to array
With the development of Internet technology, we often need to process JSON data in PHP. Normally, we convert JSON data into PHP arrays for easy manipulation and processing. This article will introduce the json_decode() function and some tips and issues in using the json_decode() function, and give some usage examples.
1. json_decode() function
json_decode() function is a method used in PHP to convert a JSON format string into a PHP object or array. The specific syntax is as follows:
mixed json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0);
Function description:
$json
: required. The JSON format string to convert. $asso
c: Optional. The default is false
, which means an object is returned; if set to true
, an array is returned. $depth
: Optional. Specifies the recursion depth, the maximum depth is 512. $options
: Optional. To set options when decoding JSON, see [json_decode() function description](https://www.php.net/manual/en/function.json-decode.php) for details. 2. Convert JSON to PHP array
We convert JSON to PHP array. When using the json_decode()
function, just $ The assoc
parameter can be set to true
. The following is an example of converting JSON data to a PHP array:
$json_str = '{"name": "Amy", "age": 20, "job": "Programmer"}'; $arr = json_decode($json_str, true); print_r($arr); // 输出 ["name"=>"Amy", "age"=>20, "job"=>"Programmer"]
3. Convert JSON to a PHP object
In some scenarios, we prefer to use PHP objects to process JSON data . The code example for converting JSON to PHP object is as follows:
$json_str = '{"name": "Tom", "age": 25, "job": "Designer"}'; $obj = json_decode($json_str); echo $obj->name; // 输出 Tom echo $obj->age; // 输出 25 echo $obj->job; // 输出 Designer
4. Some usage skills
When usingjson_decode ()
function, we need to pay attention to handling the conversion failure. If the JSON string does not conform to the specification, the json_decode()
function returns null
. We can use the json_last_error()
function to determine whether the conversion is successful. The specific example is as follows:
$json_str = '{name: Amy}'; $obj = json_decode($json_str); if (json_last_error() == JSON_ERROR_NONE) { echo '转换成功'; } else { echo '转换失败'; // 输出 转换失败 }
Unicode is often used in JSON Encoding represents special characters that need to be transcoded when parsing. When using the json_decode()
function, we can set the $options
parameter to JSON_UNESCAPED_UNICODE
, which means that Unicode will not be transcoded. Specific code examples are as follows:
$json_str = '{"name": "\u5f20\u4e09"}'; $arr = json_decode($json_str, true, 512, JSON_UNESCAPED_UNICODE); echo $arr['name']; // 输出 张三
The above are methods and techniques for converting JSON to PHP arrays or objects. Hope this article is helpful to you!
The above is the detailed content of How to convert json php object to array. For more information, please follow other related articles on the PHP Chinese website!