How to convert php json string to array

PHPz
Release: 2023-04-20 15:13:26
Original
543 people have browsed it

As a PHP programmer, we often need to convert JSON strings into arrays for data operations. In this article, we will discuss how to implement this functionality using PHP.

First of all, we need to understand what is JSON (JavaScript Object Notation)? JSON is a lightweight data exchange format, consisting of data types in the JavaScript language, specified in RFC4627. It has good legibility and easy learning characteristics, and is widely used in web applications.

PHP provides the json_decode() function, which can convert a JSON string into a PHP array. The specific sample code is as follows:

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

In the above code, $json_str stores a JSON string, which contains information about the three fields of name, age and gender. When we use the json_decode() function, we need to set the second parameter to true (the default value is false), so that the returned result is an associative array. If we do not pass in the second parameter or pass in false, the PHP object is returned.

In addition to regular JSON strings, another common situation is JSON array (JSON Array). In this case, we need to convert the JSON array string into a PHP array. It should be noted that it needs to be set to an unassociated array during conversion. The sample code is as follows:

$json_str = '[{"name":"Tom","age":20,"gender":"male"},{"name":"Jim","age":22,"gender":"male"}]';
$arr = json_decode($json_str, true);
print_r($arr);
Copy after login

In the above code, $json_str stores a JSON array string, which contains two elements. Each element is an associative array, containing name, age and gender respectively. field information. It is also important to note that when using the json_decode() function, you need to set the second parameter to true.

In actual development, we can also obtain information about json_decode() function calling errors by using the $json_last_error() function. The sample code is as follows:

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

if (json_last_error() > 0) {
    echo "Error decoding JSON: ".json_last_error_msg();
} else {
    print_r($arr);
}
Copy after login

In the above code, we artificially omit the last square bracket of the JSON array string, which will cause the json_decode() function to fail to parse. In this case, you can use the json_last_error() function to determine the error type and obtain the error information through the json_last_error_msg() function.

In short, converting a JSON string into an array in PHP is a very basic operation and is often used in actual development. By using the json_decode() function introduced above, we can easily convert the JSON string into a PHP array for subsequent data processing.

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

source:php.cn
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