Home > Article > Backend Development > How to convert JSON text format into PHP array
In PHP, we usually use JSON format to store and transmit data. JSON is a lightweight data exchange format that is easy to read and write, and can be easily exchanged between different programming languages. Although we can use JSON as a format for storing and transmitting data, in actual applications, we often need to convert JSON formatted text into a PHP array so that we can use PHP's array functions to manipulate the data. In this article, we will learn how to convert JSON text format into PHP array.
The format of JSON is a plain text format, which consists of key-value pairs, each key-value pair is separated by a comma, and the key-value pair is separated by a colon. The format of using JSON to store data is very flexible. Various complex data structures such as arrays and objects can be used, and the text format of JSON is very similar to the format of PHP arrays. Therefore, converting JSON text into a PHP array is very simple.
In PHP, we can use the json_decode() function to convert JSON text into a PHP array. The following is a sample code that uses the json_decode() function to convert JSON text into a PHP array:
<?php // JSON 文本数据 $json_data = '{"name":"Tom","age":20,"gender":"male"}'; // 将 JSON 文本转换成 PHP 数组 $array_data = json_decode($json_data, true); // 输出 PHP 数组 print_r($array_data); ?>
In the above code, we first define a variable $json_data that contains the JSON text, which contains a Object, which contains three key-value pairs, namely name, age and gender. Next, we use the json_decode() function to convert the JSON text into a PHP array and save the returned array in the $array_data variable. Finally, we use the print_r() function to output the PHP array so we can see the converted results.
When we run the above code, we can see the following output:
Array ( [name] => Tom [age] => 20 [gender] => male )
The output shows that we successfully converted the JSON text into a PHP array. When calling the json_decode() function, we pass the second parameter true, which specifies that we will return an associative array instead of an object. If we don't pass the second argument, the returned result will be an object instead of an array.
In addition to converting ordinary JSON text into a PHP array, we can also convert a JSON text containing multiple objects into an array containing multiple PHP arrays. For example, suppose we have a JSON text containing multiple objects as shown below:
[ {"name":"Tom","age":20,"gender":"male"}, {"name":"Alice","age":22,"gender":"female"}, {"name":"Bob","age":25,"gender":"male"}, {"name":"Jack","age":30,"gender":"male"} ]
We can convert this JSON text into a PHP array using the json_decode() function. When calling the json_decode() function, we still need to pass the second parameter true in order to return an associative array. The following is a sample code to convert a JSON text containing multiple objects into a PHP array:
<?php // 包含多个对象的 JSON 文本数据 $json_data = '[{"name":"Tom","age":20,"gender":"male"},{"name":"Alice","age":22,"gender":"female"},{"name":"Bob","age":25,"gender":"male"},{"name":"Jack","age":30,"gender":"male"}]'; // 将 JSON 文本转换成 PHP 数组 $array_data = json_decode($json_data, true); // 输出 PHP 数组 print_r($array_data); ?>
When we run the above code, we can see the following output:
Array ( [0] => Array ( [name] => Tom [age] => 20 [gender] => male ) [1] => Array ( [name] => Alice [age] => 22 [gender] => female ) [2] => Array ( [name] => Bob [age] => 25 [gender] => male ) [3] => Array ( [name] => Jack [age] => 30 [gender] => male ) )
Output results It shows that we successfully converted a JSON text containing multiple objects into an array containing multiple PHP arrays.
Summary:
In PHP, it is very simple to use the json_decode() function to convert JSON text into a PHP array. Whether you are converting a plain JSON text into a PHP array, or converting a JSON text containing multiple objects into an array containing multiple PHP arrays, it can be easily done using the json_decode() function. Therefore, when we need to manipulate JSON data, we can give priority to using PHP arrays to process data for better data manipulation.
The above is the detailed content of How to convert JSON text format into PHP array. For more information, please follow other related articles on the PHP Chinese website!