Home  >  Article  >  Backend Development  >  How to convert a JSON string to an array in PHP

How to convert a JSON string to an array in PHP

PHPz
PHPzOriginal
2023-04-23 09:16:15581browse

In PHP, we often need to convert JSON strings into arrays. This helps us operate and process the data. After all, JSON is a lightweight data exchange format, and many network interfaces return data in JSON format.

So, how to convert a JSON string into an array in PHP? Let’s introduce it below.

  1. Use the json_decode() function

In PHP, we can use the json_decode() function to convert a JSON string into a PHP array. The syntax of the json_decode() function is as follows:

mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )

Among them, the parameter $json is the JSON string to be converted. The $assoc parameter is optional and the default value is FALSE. If set to TRUE, the converted array will be an associative array. The $depth parameter is optional and indicates the depth of recursively parsing JSON strings. JSON strings greater than this depth will be converted to NULL. The $options parameter is optional and represents JSON parsing options, which can be set to JSON_BIGINT_AS_STRING, JSON_OBJECT_AS_ARRAY or JSON_THROW_ON_ERROR.

The following is an example of converting a JSON string into an array:

$json_str = '{"name": "Tom", "age": 20, "gender": "male"}';
$arr = json_decode($json_str, true);

print_r($arr);

Output result:

Array
(
    [name] => Tom
    [age] => 20
    [gender] => male
)
  1. Solving the problem of Chinese garbled characters

If the JSON string contains Chinese characters, garbled characters may appear in the converted array. At this time, we need to use the JSON_UNESCAPED_UNICODE option to exclude all non-ASCII characters from hexadecimal encoding.

The following is an example of using the JSON_UNESCAPED_UNICODE option:

$json_str = '{"name": "汤姆", "age": 20, "gender": "男"}';
$arr = json_decode($json_str, true, 512, JSON_UNESCAPED_UNICODE);

print_r($arr);

Output results:

Array
(
    [name] => 汤姆
    [age] => 20
    [gender] => 男
)
  1. Solving the problem of special characters in JSON strings

Sometimes, the JSON string contains special characters, such as content consisting of < or > or & symbols. These special characters need to be escaped in the JSON syntax specification, but some JSON strings returned by the interface are not escaped.

At this time, we need to use the JSON_UNESCAPED_SLASHES option to ensure that these special characters will not be escaped.

The following is an example of using the JSON_UNESCAPED_SLASHES option:

$json_str = '{"name": "<Tom>", "age": 20, "gender": "&male&"}';
$arr = json_decode($json_str, true, 512, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

print_r($arr);

Output result:

Array
(
    [name] => <Tom>
    [age] => 20
    [gender] => &male&
)

The above is how to convert a JSON string into an array in PHP. I hope it will be helpful to you Helps.

The above is the detailed content of How to convert a JSON string to an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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