Ajax requests that PHP return JSON object data containing numeric indexes and string indexes, and the order in which data is fetched in the for-in loop.

Source: Internet
Author: User


//array data format in PHPArray([All]=Array([title]=All [room_promotion_id]=All ) [Best_available_rate]=Array([title]=Best Available rate [room_promotion_id]=best_available_rate) [+] =Array([room_promotion_id]= 30[Version]= 1[Date_from]= 2014-08-05[date_to]= 2014-09-30[Night_no]= 1[Discount_stype]= 1[Discount_rate]= 10[Discount_night]= 0[title]= Summer Discount-Premier Suite [summary]= <p>bring your family along to relax in the Summer! Enjoy a 10% off whenever you book Premier suite.</p>[Before_day]= 0 ))
// json_encode () output data, which is also the JSON data object returned by Ajax {' All ': {' title ': ' All ', ' room_promotion_id ': ' All '},
"Best_available_rate": {"title": "Best Available Rate", "room_promotion_id": "Best_available_rate"},
"A": {"room_promotion_id": "1", "Version": "" "," "2014-08-05", "Date_from": "2014-09-30", "Night_no": "1", " Discount_stype ":" 1 ",
"Discount_rate": "Ten", "Discount_night": "0", "title": "Summer discount-premier Suite",
"Summary": "<p>\r\n\tbring your family along to relax in the summer! Enjoy a 10% off whenever you book Premier suite.<\/p>\r\n "," Before_day ":" 0 "}}
//JavaScript codefunctionajaxprmotion () {varCheck_in_hidden = $ (' #check_in_hidden '). Val (); varCheck_out_hidden = $ (' #check_out_hidden '). Val (); varAdults_num = $ (' #adult '). attr (' Selected ', ' selected '). Val (); varChild_num = $ (' #child '). attr (' Selected ', ' selected '). Val (); varpromotion_opt = ' '; varUrl= "reservation.php?action_type=mobile_ajax&check_in=" +check_in_hidden+ "&check_out=" +check_out_hidden + "&adult=" +adults_num+ "&child=" +child_num+ "&upd=1"; $.ajax ({url:url, type:"Get", DataType:"JSON", Success:function(Result) {//$ ("select[name=promotion_id]"). Find ("option"). Remove (). End (). Append ("<option value= ' 0 ' >__ ($lang _ text.best_rate) __</option> ");$ ("select[name=promotion_id]"). Find ("option")). Remove (); varpromotion_opt = ' '; if(result!= ' &&result) {/X
Using the for (var variablename in object) {} loops out the order of data with the order of the data in the printed array in PHP and the order of the data after Json_encode () is different
The first cycle of data that is indexed to 30, then all and best_available_rate, does not know what this is all about.
Because the order of the arrays in PHP is fixed, the data from Json_encode () is the same as the order in the PHP array, but why is the use of in-in data in JavaScript
But the order is different? There are only two reasons: do you sort the transmitted JSON format data in 1.jquery? 2. It's either in the for-in problem.
x/
for(varPromotioninchresult) {promotion_opt+ = ' <option value= ' ' +result[promotion][' room_promotion_id ']+ ' > ' + result[promotion][' title ']+ ' </option > '; } } $("SELECT[NAME=PROMOTION_ID]"). Append (promotion_opt); } });}


So, I wrote a piece of JavaScript code myself to test it, and the reason for the problem was that it was in the JavaScript for.

The test code is as follows:

<script type= "Text/javascript" >    var json_data = {"All": {' title ': ' All ', ' room_promotion_id ': ' All '  "Best_room_rate":    {"title": "Best_room_rate", "roompromotion_id": "Best_room_rate"},                            " ": {" title ":" Summer "," room_promotion_id ":" + "}                            ;              for (var in json_data) {    alert (promotion); // 30->all->best_room_rate}
</script>

For the above code, I changed the number "30" to "summer rate" with the following code:

<script type= "Text/javascript" >var json_data = {"All": {' title ': ' All ', ' room_promotion_id ': ' All '} ,                             "best_room_rate": {"title": "Best_room_rate", "roompromotion_id": "Best_room_rate"},                            " Summer Rate ": {" title ":" Summer "," room_promotion_id ":" + "}                            ;              for (var in json_data) {    alert (promotion); // The result of this loop output is exactly the order in the Json_data data defined above }</script>

From the above code comparison, I come to the following conclusion, perhaps my conclusion is not necessarily completely correct and scientific.

Conclusion: When the JSON format data contains both a numeric index and a text index, the data that must be a numeric index is first taken out in the for in loop, and then the data is taken out of the text index sequentially.

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.