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 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入