Home  >  Article  >  Backend Development  >  How to convert json string to array in php

How to convert json string to array in php

PHPz
PHPzOriginal
2023-04-20 13:52:31907browse

In PHP, we often need to deal with JSON format. JSON is a lightweight data exchange format. In addition to data transmission between HTTP requests and responses, it is also supported by an increasing number of programming languages.

In PHP, we can use the json_decode() function to convert a JSON string into an array. This function accepts two parameters. The first parameter is a variable containing a JSON format string, and the second parameter is a Boolean value used to determine whether to return the converted object in the form of an array.

For example, the following code converts a JSON-formatted string into an associative array:

$json = '{"name": "John", "age": 30, "city": "New York"}';
$arr = json_decode($json, true);

print_r($arr);

Output result:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

In this example, we use a string containing three JSON string of attributes (name, age, and city) and pass it to the json_decode() function. This function returns an array corresponding to the JSON string, which we save in the variable $arr. Finally, use the print_r() function to output the contents of $arr.

It should be noted that you may encounter some problems when converting a JSON string to an array. For example, if the JSON string contains characters that do not comply with JSON specifications, the conversion will fail. In addition, if the format of the JSON string is incorrect, it may also cause the conversion to fail.

In addition, if the JSON string contains non-ASCII characters, you may need to use unicode encoding to process it. In this case, we can pass the JSON string to the json_decode() function and then use the json_last_error() function to see if the decoding was successful.

For example, the following code converts a JSON string containing non-ASCII characters into an array:

$json = '{"name": "张三", "age": 30, "city": "北京"}';
$arr = json_decode($json, true);

if(json_last_error() === JSON_ERROR_NONE) {
    print_r($arr);
} else {
    echo "解码失败";
}

Output result:

Array
(
    [name] => 张三
    [age] => 30
    [city] => 北京
)

In this example, we use an array containing A JSON string of Chinese characters and pass it to the json_decode() function. At the same time, we used the json_last_error() function to check whether the decoding was successful. If there is no error, the contents of $arr are output. If decoding fails, an error message is output.

To sum up, using the json_decode() function to convert a JSON string into an array is one of the common operations in PHP. When converting, you need to pay attention to the format and content of the JSON string to ensure correct decoding results.

The above is the detailed content of How to convert json string to 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