PHP uses JSON

Source: Internet
Author: User

  This article is mainly to illustrate the json_encode use of a need to pay attention to the details, whether the return of an array or JSON, does not mean that this must be a bad return result, the current end of the array required, the array is the correct result, and vice versa

With Ajax rampant today, JSON is logically a very important tool for front and back interaction. For PHP, it is common to use the Json_encode method to convert a PHP group number into a JSON string that can be parsed by the front end, as described in the PHP manual, but is that the case? Take a look at the following code: The   code is as follows: $a = array (' Jack ', ' Sam ', ' Tom '); echo Json_encode ($a);   When JavaScript requests to the code above, PHP parses the array $a as a JSON string and returns to the front end, but in fact the return result on the front end is an array. The code is as follows: ["Jack",   "Sam",   "Tom"]   This result may not be expected for the front end, and for JavaScript, arrays and JSON are very closely related, and can even simply understand the JSON is an associative array, but that does not mean that the two can be equated, for example, JSON has no length attribute and cannot be indexed numerically, JSON is a key-value pair, and JavaScript's array is strictly not "key", which is quite different from PHP. So why is Json_encode's return result an array?   The array in the PHP code at the beginning of this article is a strictly numeric indexed array, and the Json_encode method returns an array string when processing such an array, and it needs to satisfy two conditions: 1. Array of numeric indices, 2. The index value starts at 0. This means that the following code will return the same result: The     code is as follows: $b = Array (' 0 ' => ' Jack ', ' 1 ' => ' Sam ', ' 2 ' => ' Tom '); echo Json_encode ($b);   Any one of these two conditions is not satisfied, then the Json_encode method really returns the JSON string: The code is as follows: $c = Array (' person-1 ' => ' Jack ', ' person-2 ' => ' Sam ', ' person-3 ' => ' Tom '); echo Json_encode ($c);   This is the result of the front end: The code is as follows: {' person-1 ': ' Jack ', ' person-2 ': ' Sam ', ' person-3 ': ' Tom '};  

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.