//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.