Home >Backend Development >PHP Problem >How to convert json to array in php
When developing web applications, JSON in PHP improves the efficiency of data transmission and storage a lot. JSON (JavaScript Object Notation) is a lightweight data format used for data exchange that has a legible, easy-to-write and understand syntax and is faster than XML and can pass data into JavaScript.
PHP provides many built-in functions that can convert JSON to arrays. In this article, we will introduce some commonly used JSON decoding functions.
json_decode() function is one of the most commonly used functions to convert JSON to a PHP array. It is one of the most commonly used JSON decoding functions in PHP. Using this function, you can easily decode JSON formatted data into a PHP array.
The following is an example: First create a JSON format string named $json:
$json = '{"name":"Tom", "age":25, " city":"Beijing"}';
Pass this JSON string to the json_decode() function and store it in a variable to convert it to a PHP array:
$result = json_decode($json, true);
If the second parameter is true, an array will be returned instead of an object.
It is now possible to access properties of JSON data stored in another string by accessing a PHP array.
echo $result['name']; // Tom
echo $result['age']; // 25
The following is an example of a nested array in JSON format:
$json = '{"person": [{"name": "Tom" , "age": 25}, {"name": "Lucy", "age": 26}]}';
You can use the json_decode() function to decompose this JSON data into a PHP array, As shown below:
$result = json_decode($json, true);
You can now access multiple objects in the data through the $array array to get the values:
echo $result'person'['name']; // Tom
echo $result'person'['name']; // Lucy
The following is a Example of nested objects in JSON format:
$json = '{"person": {"name": "Tom", "age": 25}}';
You can use json_decode () function breaks this JSON data into a PHP array, as shown below:
$result = json_decode($json, true);
The nested can now be accessed through the $array array JSON object to get the value in it:
echo $result'person'; // Tom
echo $result'person'; // 25
Use the json_last_error() function to check whether your json_decode() function decoded the JSON data.
If there is no decoding, this function will return an error code, which you can use to determine the cause of the error in JSON parsing.
The following is an example:
$json = '{"name": "Tom,}';
$result = json_decode($json);
if(json_last_error() == JSON_ERROR_SYNTAX){
echo "JSON format error";
}
This will output "JSON format error" because JSON The format is invalid.
Like the json_decode() function, there is also a function in PHP that can convert the PHP data structure into JSON format, which is json_encode() function.
json_encode() function converts a PHP array into a JSON format string.
The following is a function that uses the json_encode() function to convert a PHP array into a JSON format string Example:
$person = array(
'name' => 'Tom',
'age' => 25,
'city' => 'Beijing'
);
echo json_encode($person);
// {"name":"Tom","age" :25,"city":"Beijing"}
Summary
Converting JSON data to PHP arrays is a common task when developing web applications. This article introduces many built-in functions , such as json_decode() and json_encode(), these functions can be used to decode and use JSON data in PHP.
By formatting data as JSON, you can easily pass information from one application to another An application that ensures that the format of the data is always preserved and that data can be efficiently transferred and stored between PHP and JavaScript.
The above is the detailed content of How to convert json to array in php. For more information, please follow other related articles on the PHP Chinese website!