Home > php教程 > php手册 > php之json_encode和json_decode

php之json_encode和json_decode

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 16:05:09
Original
1130 people have browsed it

1.当array是一个从0开始的连续数组时,json_encode出来的结果是一个由[]括起来的字符串

而当array是不从0开始或者不连续的数组时,json_encode出来的结果是一个由{}括起来的key-value模式的字符串

$test = array();
$test[] = 1;
$test[] = 1;
$test[] = 1;
DEBUG(json_encode($test));
Copy after login
结果:
[1,1,1]
Copy after login

$test = array(); 
$test[] = 1; 
$test[] = 1; 
$test[] = 1; 
unset($test[0]); 
DEBUG(json_encode($test));
Copy after login
结果:
{"1":1,"2":1}
Copy after login
Copy after login

2.当字符串为[1,1,1] 这种模式时,json_decode默认解析出来的结果是一个数组,

当字符串为{"1":1,"2":1} 这种模式时,json_decode默认解析出来的结果是一个对象,此时可以设置它的第二个参数为true强制让它返回数组

 

3.由于php无法区分一维数组和二维数组,才会出现以上情况,因为使用json编码时推荐将第二个参数设置为true

Statement of this Website
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
Latest Issues
Backslash present in Json
From 1970-01-01 08:00:00
0
0
0
Get: Transfer JSON data
From 1970-01-01 08:00:00
0
0
0
mysql storage json error
From 1970-01-01 08:00:00
0
0
0
javascript - Problems with displaying json data
From 1970-01-01 08:00:00
0
0
0
Find matching integers in JSON.
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template