How to convert json php object to array

PHPz
Release: 2023-04-18 09:58:01
Original
529 people have browsed it

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);
Copy after login

Function description:

  • $json: required. The JSON format string to convert.
  • $assoc: 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"]
Copy after login

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
Copy after login

4. Some usage skills

  1. Judge whether the conversion is successful

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 '转换失败'; // 输出 转换失败
}
Copy after login
  1. Processing Unicode encoding

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']; // 输出 张三
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!