为什么JS对象总是按密钥排序?

时间:2017-01-20 16:06:24

标签: javascript php

我使用msg_data msg_time子句从MySQL查询中获取ORDER BY。 然后我使用foreach循环来创建像这样的对象的JS对象:

<script type="text/javascript">
    var active_swaps = {
    <?php if (is_array($msg_data) || $msg_data instanceof Traversable):?>
        <?php foreach($msg_data as $msg_item):?>
            <?php echo $msg_item['id'];?> : <?php echo json_encode($msg_item);?>,
        <?php endforeach;?>
    <?php endif;?>
    };
</script>

如果我使用$msg_data检查var_dump,我会:

array (size=3)
  0 => 
    array (size=20)
      'id' => string '5' (length=1)
      'user1_id' => string '1' (length=1)
      'user2_id' => string '2' (length=1)
      'hasto' => string '2' (length=1)
      'requested' => string '2017-01-20 14:28:00' (length=19)
      'accepted' => null
      'swapped1' => null
      'swapped2' => null
      'rejected' => null
      'rejected_by' => null
      'swap_seen' => null
      'msg_id' => string '23' (length=2)
      'sender_id' => string '1' (length=1)
      'msg' => string 'awdwadwd' (length=8)
      'msg_time' => string '2017-01-20 15:58:06' (length=19)
      'swap_id' => string '5' (length=1)
      'seen' => null
      'user1username' => string 'user1' (length=5)
      'user2username' => string 'user2' (length=5)
      'online' => string 'false' (length=5)
  1 => 
    array (size=20)
      'id' => string '7' (length=1)
      'user1_id' => string '1' (length=1)
      'user2_id' => string '2' (length=1)
      'hasto' => string '2' (length=1)
      'requested' => string '2017-01-20 15:57:51' (length=19)
      'accepted' => null
      'swapped1' => null
      'swapped2' => null
      'rejected' => null
      'rejected_by' => null
      'swap_seen' => null
      'msg_id' => string '22' (length=2)
      'sender_id' => string '1' (length=1)
      'msg' => string 'awdawd' (length=6)
      'msg_time' => string '2017-01-20 15:58:00' (length=19)
      'swap_id' => string '7' (length=1)
      'seen' => null
      'user1username' => string 'user1' (length=5)
      'user2username' => string 'user2' (length=5)
      'online' => string 'false' (length=5)
  2 => 
    array (size=20)
      'id' => string '6' (length=1)
      'user1_id' => string '1' (length=1)
      'user2_id' => string '2' (length=1)
      'hasto' => string '2' (length=1)
      'requested' => string '2017-01-20 15:11:58' (length=19)
      'accepted' => null
      'swapped1' => null
      'swapped2' => null
      'rejected' => null
      'rejected_by' => null
      'swap_seen' => null
      'msg_id' => string '20' (length=2)
      'sender_id' => string '1' (length=1)
      'msg' => string 'awdadwdwa' (length=9)
      'msg_time' => string '2017-01-20 15:46:44' (length=19)
      'swap_id' => string '6' (length=1)
      'seen' => null
      'user1username' => string 'user1' (length=5)
      'user2username' => string 'user2' (length=5)
      'online' => string 'false' (length=5)

然而在JS console.log(active_swaps);中返回:

Object {5: Object, 6: Object, 7: Object}

无论我做什么,总是按键(swap_id)排序!如果有人能解释为什么会发生这种情况,我将不胜感激?

1 个答案:

答案 0 :(得分:0)

好吧,看起来你正在创建一个以$msg_item['id']为关键字的对象:

var active_swaps = {
<?php if (is_array($msg_data) || $msg_data instanceof Traversable):?>
    <?php foreach($msg_data as $msg_item):?>
        <?php echo $msg_item['id'];?> : <?php echo json_encode($msg_item);?>,
    <?php endforeach;?>
<?php endif;?>
};

在你的循环中被解释为:

var active_swaps = {
    5 : 'JSON Object',
    6 : 'JSON Object',
    7 : 'JSON Object',
};

(注意:为了便于阅读,我在此处替换了'JSON Object'的实际JSON字符串)

你应该创建一个数组而不是一个对象(这意味着数字和0索引键(感谢@CharlotteDunois指出那个)),如下:

var active_swaps = [];
<?php if (is_array($msg_data) || $msg_data instanceof Traversable):?>
    <?php foreach($msg_data as $msg_item):?>
        active_swaps.push(<?php echo json_encode($msg_item);?>);
    <?php endforeach;?>
<?php endif;?>

这相当于:

var active_swaps = [];
active_swaps.push('JSON Object');
active_swaps.push('JSON Object');
active_swaps.push('JSON Object');

然后console.log(active_swaps)应返回以下内容:

[Object, Object, Object]

编辑:将输出保留为对象

您可以使用$msg_data的密钥作为JS对象密钥:

var active_swaps = {
<?php if (is_array($msg_data) || $msg_data instanceof Traversable):?>
    <?php foreach($msg_data as $key=>$msg_item):?>
        <?php echo $key;?> : <?php echo json_encode($msg_item);?>,
    <?php endforeach;?>
<?php endif;?>
};

console.log(active_swaps)的输出现在是Object {0: Object, 1: Object, 2: Object},据说保持与PHP数组相同的顺序(即5,然后是7,然后是6)