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 }
In PHP, we can use thejson_decode
function 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 isfalse
or omitted, the returned value is an object.
So, we can use thejson_decode
function 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);
The running result is:
Array ( [name] => Alice [age] => 30 [height] => 1.65 )
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_decode
function 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);
The running result is:
Array ( [name] => Alice [age] => 30 [height] => 1.65 )
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:
json_decode
function to convert the JSON object into a PHP object, and then use Cast to convert it to an array.json_decode
function 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!