In php, array and json conversion are simple. We only need to use json_encode () and json_decode (). It is easy to understand that json_encode () is to convert the PHP array to Json. On the contrary, json_decode () converts Json to 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]
In this way, json can be converted into an array, and the key remains in the original format.
| The Code is as follows: |
Copy code |
$ Json = '{"name": "zhangsan", "age": 20, "sex": "nan "}'; Print_r (json_decode ($ json, true )); |
After json data is parsed, it will become the following Array
Array
(
[Name] => zhangsan
[Age] => 20
[Sex] => nan
)