drbxr86044 2018-07-10 02:31
浏览 419
已采纳

对内部数组时间戳进行排序

I have an array with multiple elements that looks like this:

  {
"name": "DR",
"data": [
  [
    "1508112000000",
    4
  ],
  [
    "1534204800000",
    1
  ]
],
"type": "areaspline"
},
{
    "name": "SIT",
    "data": [
      [
        "1508112000000",
        4
      ],
      [
        "1534204800000",
        1
      ],
      [
        "1506384000000",
        3
      ],
      [
        "1534204800000",
        1
      ],
      [
        "1531094400000",
        1
      ],
      [
        "1528502400000",
        1
      ]
    ],
    "type": "areaspline"
  },

This is the exact format I use to send data into high charts, however, the problem is that the chart breaks if the timestamps inside each environment (DR, SIT) are not in order.

How can I sort the 'data' inside each environment by timestamp?

This JSON is generated in PHP and sent through to JavaScript. So I would like to know how to sort the data inside either PHP or JavaScript.

Thanks.

  • 写回答

1条回答 默认 最新

  • douba5540 2018-07-10 02:48
    关注

    This is actually rather trivial, once you know about the usort function. It allows you to define your own sorting function, so you can sort based on any factor of whatever the 2 objects it is passed.

    Note that from your example json I had to add a set of square brackets around the whole thing to get PHP to parse it with json_decode .

    <?php
    
    // note I had to add square brackets to your
    // demo json ....
    
    $json='[{
    "name": "DR",
    "data": [
      [
        "1508112000000",
        4
      ],
      [
        "1534204800000",
        1
      ]
    ],
    "type": "areaspline"
    },
    {
        "name": "SIT",
        "data": [
          [
            "1508112000000",
            4
          ],
          [
            "1534204800000",
            1
          ],
          [
            "1506384000000",
            3
          ],
          [
            "1534204800000",
            1
          ],
          [
            "1531094400000",
            1
          ],
          [
            "1528502400000",
            1
          ]
        ],
        "type": "areaspline"
      }]';
    
    $json_obj_arr=json_decode($json,true);
    
    print_r($json_obj_arr);
    print("
    
    ");
    
    // a function to handle the sorting itself
    // usort will pass it an array(timestamp,datavalue) for both
    // $a and $b so we compare the [0] element of each
    function cmp($a, $b)
    {
        if ($a[0] == $b[0]) {
            return 0;
        }
        return ($a[0] < $b[0]) ? -1 : 1;
    }
    
    
    // loop through the array, first is indexed
    for($i=0;$i<count($json_obj_arr);$i++){
        // then each one of those contains an
        // associtive array with an element named ['data'] -
        // this is an indexed array that you want sorted
        // on the [0] element
    
        usort($json_obj_arr[$i]['data'],"cmp");
    }
    
    print_r($json_obj_arr);
    print("
    
    ");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率