Home > Backend Development > PHP Tutorial > The difference between braces and curly braces in php json_encode value_PHP tutorial

The difference between braces and curly braces in php json_encode value_PHP tutorial

WBOY
Release: 2016-07-21 16:13:13
Original
925 people have browsed it

1. When array is a continuous array starting from 0, the result of json_encode is a string enclosed by []

When the array is an array that does not start from 0 or is not continuous, the result of json_encode is a string in key-value pattern enclosed by {}

Copy code The code is as follows:

$test = array();
$test[] = 1;
$test[] = 1;
$test[] = 1;
echo json_encode($test);

Result:

[1,1,1]

Copy code The code is as follows:

$test = array();
$test[] = 1;
$test[] = 1;
$test[] = 1;
unset($test[0]);
echo json_encode($test);

Result:

{"1":1,"2":1}

2. When the string is in the pattern [1,1,1], the result parsed by json_decode is an array by default,

When the string is in the pattern {"1":1,"2":1}, the result parsed by json_decode is an object by default. At this time, you can set its second parameter to true to force it to Return array

3. The above situation occurs because PHP cannot distinguish between one-dimensional arrays and two-dimensional arrays, because it is recommended to set the second parameter to true when using json encoding

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313516.htmlTechArticle1. When array is a continuous array starting from 0, the result of json_encode is an array bracketed by [] When the array is an array that does not start from 0 or is not continuous, json_encode outputs...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template