PHP json_encode value between curly braces and curly braces _php tips

Source: Internet
Author: User

1. When the array is a contiguous array starting at 0, the result of the Json_encode is a string enclosed by []

When the array is not starting at 0 or not contiguous, the result of the Json_encode is a string of key-value patterns enclosed by {}

Copy Code code as follows:

$test = Array ();
$test [] = 1;
$test [] = 1;
$test [] = 1;
echo Json_encode ($test);

Results:

[1,1,1]

Copy Code code as follows:

$test = Array ();
$test [] = 1;
$test [] = 1;
$test [] = 1;
unset ($test [0]);
echo Json_encode ($test);

Results:

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

2. When the string is [1,1,1] This pattern, the default parsing result of Json_decode is an array,

When the string is {"1": 1, "2": 1} This mode, the result of the Json_decode default resolution is an object, at which point the second argument can be set to true to force it to return an array

3. Because PHP cannot differentiate between one-dimensional and two-dimensional arrays, the above situation occurs because the second parameter is recommended to TRUE when using JSON encoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.