How to convert php json object into array

PHPz
Release: 2023-04-26 09:29:59
Original
460 people have browsed it

In PHP, we often use JSON (JavaScript Object Notation) to pass data. JSON is a lightweight data format that is easy to read, write and parse, and is suitable for data exchange between different languages. Sometimes we encounter situations where we need to convert a JSON object into an array. This article will introduce how to achieve this function in PHP.

Let’s first look at the structure of JSON. A JSON object contains multiple key-value pairs. Each key-value pair consists of a field name and a value. The field name and value are separated by colons. Different key values Separate pairs with commas and wrap them in curly braces. For example:

{ "name": "Alice", "age": 30, "height": 1.65 }
Copy after login

In PHP, we can use thejson_decodefunction to convert a JSON string into a PHP variable. The first parameter of this function is the JSON string to be decoded, and the second parameter is a Boolean value indicating whether the returned value is an associative array or a normal array. If the second parameter istrue, the returned value is an associative array; if it isfalseor omitted, the returned value is an object.

So, we can use thejson_decodefunction to convert a JSON string into a PHP object, and then use cast to convert it into an array. For example:

$json = '{"name": "Alice", "age": 30, "height": 1.65}'; $obj = json_decode($json); // 将 JSON 对象转换成 PHP 对象 $arr = (array) $obj; // 将 PHP 对象转换成数组 print_r($arr);
Copy after login

The running result is:

Array ( [name] => Alice [age] => 30 [height] => 1.65 )
Copy after login
Copy after login

The above code converts the JSON object into an array containing three elements. The key name of each element corresponds to the field name in the JSON object. The key value corresponds to the value of the field.

Of course, it will be simpler if we directly use the second parameter of thejson_decodefunction to convert it into an associative array. For example:

$json = '{"name": "Alice", "age": 30, "height": 1.65}'; $arr = json_decode($json, true); // 将 JSON 对象转换成关联数组 print_r($arr);
Copy after login

The running result is:

Array ( [name] => Alice [age] => 30 [height] => 1.65 )
Copy after login
Copy after login

The above code also converts the JSON object into an array containing three elements. The key name of each element corresponds to the field name in the JSON object. , the value of the field corresponding to the key value. The only difference is that this array is an associative array, and the key name of each element is exactly the same as the field name in the JSON object.

To summarize, there are two ways to convert a JSON object into an array:

  • First use thejson_decodefunction to convert the JSON object into a PHP object, and then use Cast to convert it to an array.
  • Directly use the second parameter of thejson_decodefunction to convert it into an associative array.

In actual development, we can choose the appropriate method according to actual needs. It should be noted that if the JSON object contains nested JSON objects or arrays, recursive processing may be required when converting to arrays.

The above is the detailed content of How to convert php json object into 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
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!