Home > Backend Development > PHP Problem > Convert json string to array php

Convert json string to array php

PHPz
Release: 2023-04-23 09:17:15
Original
252 people have browsed it

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

Among them:

  • $json: the json string that needs to be parsed;
  • $assoc: whether to return the object Convert to an associative array. If the value is TRUE, it will be converted to an associative array. The default is not to convert;
  • $depth: Set the depth of the recursion. The default is 512. In order to avoid memory overflow, you can adjust this value according to the actual situation;
  • $options: Optional parameter, you can specify options for parsing JSON.

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

Output:

<code>stdClass Object
(
   [name] => Tom
   [age] => 30
   [sex] => male
)</code>
Copy after login
Copy after login

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

Output:

<code>Array
(
   [name] => Tom
   [age] => 30
   [sex] => male
)</code>
Copy after login

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

Among them:

  • $json_string: the json string that needs to be parsed;
  • $assoc: whether to return the object Convert to an associative array. If the value is TRUE, it will be converted to an associative array. The default is not to convert;
  • $depth: Set the depth of the recursion. The default is 512. In order to avoid memory overflow, you can adjust this value according to the actual situation;
  • $options: Optional parameter, you can specify options for parsing JSON.

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

Output:

<code>stdClass Object
(
   [name] => Tom
   [age] => 30
   [sex] => male
)</code>
Copy after login
Copy after login

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!

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