Home  >  Article  >  Backend Development  >  Detailed explanation of conversion between PHP array and Json_PHP tutorial

Detailed explanation of conversion between PHP array and Json_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:58840browse

Converting arrays to json in PHP is very simple, we only need to use json_encode() and json_decode(). It is easy to understand that json_encode() converts a PHP array into Json. On the contrary, json_decode() converts Json into a PHP array.

For example:

The code is as follows
 代码如下 复制代码

$array = array("name" => "Eric","age" => 23);
 
echo json_encode($array);

Copy code

$array = array("name" => "Eric","age" => 23);

echo json_encode($array);

 代码如下 复制代码

$array = array(0 => "Eric", 1 => 23);
 
echo json_encode($array);

The program will print out:

{“name”:”Eric”,”age”:23}

 代码如下 复制代码

$json = ’{"name":"zhangsan","age":20,"sex":"nan"}’; 

print_r(json_decode($json,true)); 

Look at the following example:

The program will print out:
The code is as follows Copy code



$array = array(0 => "Eric", 1 => 23);

echo json_encode($array);

["Eric",23]

In this way, json can be converted into array form, and the key remains in the original format
Statement:
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