Home > Article > Backend Development > Convert json string to array php
In PHP, converting a JSON-formatted string into an array is a very simple process. There are two PHP built-in functions that can be used for this purpose: json_decode() and json_decode_object().
1. Use the json_decode() function
The json_decode() function is a common way to convert a JSON-formatted string into a PHP array.
Grammar:
<code>mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )</code>
Among them:
Example:
Convert JSON format string into array:
<code><?php $json_string = '{"name": "Tom", "age": 30, "sex": "male"}'; $decoded_json = json_decode($json_string); print_r($decoded_json); ?></code>
Output:
<code>stdClass Object ( [name] => Tom [age] => 30 [sex] => male )</code>
In the above code, we first Define a string in JSON format, then call the json_decode() function to convert the string into the PHP array $decoded_json, and print out the result.
If you want to convert the return result into an associative array, you need to set the $assoc parameter to TRUE:
<code><?php $json_string = '{"name": "Tom", "age": 30, "sex": "male"}'; $decoded_json = json_decode($json_string, true); print_r($decoded_json); ?></code>
Output:
<code>Array ( [name] => Tom [age] => 30 [sex] => male )</code>
As you can see from the above output, the array $decoded_json is different from the last output object because $assoc was set to TRUE at this time and converted to an associative array. If $assoc is not set, it returns an object instead of an array by default.
2. Use the json_decode_object() function
In addition to the json_decode() function, PHP also provides another way to convert JSON format strings into PHP arrays, which is json_decode_object ()function.
Grammar:
<code>object json_decode_object ( string $json_string [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )</code>
Among them:
Example:
Convert JSON format string into array:
<code><?php $json_string = '{"name": "Tom", "age": 30, "sex": "male"}'; $decoded_json = json_decode_object($json_string); print_r($decoded_json); ?></code>
Output:
<code>stdClass Object ( [name] => Tom [age] => 30 [sex] => male )</code>
In the above code, we use The json_decode_object() function converts a JSON string into a PHP array $decoded_json. Since we didn't set the $assoc parameter, it returns an object instead of an array by default.
Summary
In PHP, we can use the json_decode() function and json_decode_object() function to convert JSON format strings into PHP arrays. Both functions have their own advantages, and which one to choose depends on your specific needs. If you want the result to be an object then use json_decode_object(), if you want the result to be an array then use json_decode(). During use, you also need to adjust the values of the $depth and $options parameters according to the actual situation to avoid memory overflow.
The above is the detailed content of Convert json string to array php. For more information, please follow other related articles on the PHP Chinese website!