Php uses json_encode to encode the variable json _ PHP Tutorial

Source: Internet
Author: User
Php uses json_encode to encode the variable in json format. This article mainly describes the details that need to be paid attention to when using json_encode. no matter whether an array or json is returned, it does not mean that this must be an error returned result, when the front-end needs this article, it mainly describes the details that need to be paid attention to when using json_encode. no matter whether an array or json is returned, it does not mean that this is definitely an error returned result, when the front-end requires an array, the array is the correct result, and vice versa.

As ajax is rampant today, json is naturally an extremely important tool for front-end interaction. For PHP, the json_encode method is usually used to convert the number of PHP groups into json strings that can be parsed at the front end. this is also described in the PHP Manual. But is it true? Take a look at the following code:

The code is as follows:

$ A = array ('Jack', 'Sam ', 'Tom ');

Echo json_encode ($ );

When JavaScript requests the code above, PHP will parse the array $ a as a json string and return it to the front end, but in fact the returned result obtained at the front end is an array.

The code is as follows:

["Jack", "Sam", "Tom"]

This result may not be expected for the front-end. for JavaScript, the relationship between arrays and json is very close, and json can be simply understood as an associated array, however, this does not mean that the two can be equal. for example, json does not have the length attribute or a digital index. json is a key-value pair, while JavaScript arrays do not have a "key" strictly, this is quite different from PHP. Why is the returned result of json_encode an array?

The arrays in PHP code at the beginning of this article are strictly numerical index arrays. the json_encode method returns an array string when processing such arrays. it must satisfy both of the following conditions: 1. number index array, 2. the index value starts from 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 );

If any of the two conditions is not met, the json_encode method returns the json string:

The code is as follows:

$ C = array (

'Person-1' => 'Jack ',

'Person-2' => 'Sam ',

'Person-3' => 'Tom'

);

Echo json_encode ($ c );

The result of the front-end is as follows:

The code is as follows:

{

'Person-1': 'Jack ',

'Person-2': 'Sam ',

'Person-3': 'Tom'

};

When using javasjson_encode, a special detail should be paid attention to. no matter the returned array or json, it does not mean that this is definitely an error returned result. when the front-end needs...

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.