Ajax請求php返回json對象資料中包含有數字索引和字串索引,在for in迴圈中取出資料的順序問題

來源:互聯網
上載者:User

標籤:style   blog   color   java   使用   io   strong   資料   


//
php中的數組資料格式Array( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_available_rate] => Array ( [title] => Best Available Rate [room_promotion_id] => best_available_rate ) [30] => 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()之後輸出的資料,這也是ajax返回的json資料對象{"all":{"title":"ALL","room_promotion_id":"all"},
"best_available_rate":{"title":"Best Available Rate","room_promotion_id":"best_available_rate"},
"30":{"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>\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代碼function ajaxPrmotion(){    var check_in_hidden = $(‘#check_in_hidden‘).val();    var check_out_hidden = $(‘#check_out_hidden‘).val();    var adults_num = $(‘#adult‘).attr(‘selected‘,‘selected‘).val();    var child_num  = $(‘#child‘).attr(‘selected‘,‘selected‘).val();    var promotion_opt =‘‘;    var url= "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();            var promotion_opt =‘‘;            if (result!=‘‘ && result){                /×
                使用for(var variablename in object){}迴圈出來的資料順序與php中列印輸出的數組中資料的順序和json_encode()後的資料順序是不同
                最先迴圈取到的索引為30的資料,然後是all和best_available_rate,不知道這個究竟是怎麼一回事。
                因為在php中的數組中的順序已經固定好了,json_encode()出來的資料也是和php數組中的順序相同,但是為何在javascript中使用for in出來的資料
                順序反而不相同呢?只有兩個原因: 1.jquery中對傳輸的json格式資料進行了排序嗎?2.要麼就是在for in發生的問題了
               ×/
for(var promotion in result){ promotion_opt += ‘<option value="‘ +result[promotion]["room_promotion_id"]+‘">‘+ result[promotion]["title"]+‘</option>‘; } } $("select[name=promotion_id]").append(promotion_opt); } });}


所以,我自己寫一段javascript代碼來進行測試,發現問題的原因是在javascript的for in上面.

測試代碼如下:

<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"},                            "30":{"title":"Summer Room rate" , "room_promotion_id":"30"}                            };            for(var promotion in json_data){    alert(promotion);// 30->all->best_room_rate}
</script>

針對上面的代碼,我將"30"這個數字修改為"summer rate",代碼如下:

<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 rate" , "room_promotion_id":"30"}                            };            for(var promotion in json_data){    alert(promotion);//這次迴圈輸出的結果完全就是按照上面定義的json_data資料中順序}</script>

從上面的代碼比較,我得出下面的結論,也許我的結論不一定是完全正確和合乎科學的。

結論:當json格式資料中既有包含有數字索引和文字索引時,那麼在for in迴圈中首先取出來必定是數字索引的資料,然後就是按順序取出文字索引的資料。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.