In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to achieve php using json_encode on variable json coding", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "how to achieve php using json_encode on variable json coding" it!
Today, when ajax is rampant, json has naturally become an extremely important tool for front-and back-end interaction. For PHP, the json_encode method is usually used to convert a number of PHP groups into json strings that can be parsed by the front end, which is also described in the PHP manual, but is this really the case? Take a look at the following code:
The copy code is as follows:
A = array ('Jack',' Sam', 'Tom')
Echo json_encode ($a)
When JavaScript requests the above code, PHP parses the array $an into a json string and returns it to the front end, but in fact the return result from the front end is an array.
The copy code is as follows:
["Jack", "Sam", "Tom"]
This result may not be expected for the front end, for JavaScript, the relationship between array and json is very close, and even json can be simply understood as an associative array, but this does not mean that the two can be equated, for example, json has no length attribute, nor can it be indexed numerically, json is a key-value pair, and strictly speaking, JavaScript's array does not have a "key", which is quite different from PHP. So why is the return result of json_encode an array?
The array in the PHP code at the beginning of this article is a strictly numeric indexed array. The json_encode method returns an array string when dealing with such an array, which needs to meet two conditions at the same time: 1. Digital indexed array, 2. The index value starts at 0. This means that the following code returns the same result:
The copy code is as follows:
B = array (
'0' = >' Jack'
'1' = >' Sam'
'2' = >' Tom'
);
Echo json_encode ($b)
If either of these two conditions is not met, then the json_encode method actually returns the json string:
The copy code is as follows:
C = array (
'person-1' = >' Jack'
'person-2' = >' Sam'
'person-3' = >' Tom'
);
Echo json_encode ($c)
At this time, the result of the front end is as follows:
The copy code is as follows:
{
'person-1': 'Jack'
'person-2': 'Sam'
'person-3': 'Tom'
}
Thank you for your reading, the above is "how to achieve php using json_encode on variable json coding" content, after the study of this article, I believe you on how to achieve php using json_encode on variable json coding this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.