In PHP, arrays and JSON transformations are simple, as long as we use Json_encode () and Json_decode (). It is easy to understand that Json_encode () is the conversion of PHP arrays into JSON. Instead, Json_decode () converts the JSON into a PHP array.
For example:
| The code is as follows |
Copy Code |
$array = Array ("name" = "Eric", "age" = 23);
echo Json_encode ($array); |
The program will print out:
{"Name": "Eric", "Age": 23}
Let's look at the following example:
| The code is as follows |
Copy Code |
$array = Array (0 = "Eric", 1 = 23);
echo Json_encode ($array); |
The program will print out:
["Eric", 23]
This allows you to convert the JSON into an array form, and the key remains in its original format
| The code is as follows |
Copy Code |
$json = ' {' name ': ' Zhangsan ', ' age ': ', ' sex ': ' nan '} '; Print_r (Json_decode ($json, true)); |
This JSON data is parsed and becomes an array like this
Array
(
[Name] = Zhangsan
[Age] = 20
[Sex] = Nan
)
http://www.bkjia.com/PHPjc/628751.html www.bkjia.com true http://www.bkjia.com/PHPjc/628751.html techarticle in PHP, arrays and JSON transformations are simple, as long as we use Json_encode () and Json_decode (). It is easy to understand that Json_encode () is the conversion of PHP arrays into JSON. On the contrary, Json_decode () is ...