排序内部数组时间戳
Sort internal array timestamps
我有一个包含多个元素的数组,如下所示:
{
"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"
},
这是我用来将数据发送到高级图表的确切格式,但是,问题是如果每个环境(DR、SIT)内的时间戳不按顺序,图表就会中断。
如何按时间戳对每个环境中的 'data' 进行排序?
此 JSON 在 PHP 中生成并发送到 JavaScript。所以我想知道如何对 PHP 或 JavaScript.
中的数据进行排序
谢谢。
一旦您了解 usort
函数,这实际上是相当微不足道的。它允许您定义自己的排序函数,因此您可以根据传递的 2 个对象的任何因素进行排序。
请注意,根据您的示例 json,我必须在整个内容周围添加一组方括号才能使 PHP 解析为 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("\n\n");
// 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("\n\n");
?>
我有一个包含多个元素的数组,如下所示:
{
"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"
},
这是我用来将数据发送到高级图表的确切格式,但是,问题是如果每个环境(DR、SIT)内的时间戳不按顺序,图表就会中断。
如何按时间戳对每个环境中的 'data' 进行排序?
此 JSON 在 PHP 中生成并发送到 JavaScript。所以我想知道如何对 PHP 或 JavaScript.
中的数据进行排序谢谢。
一旦您了解 usort
函数,这实际上是相当微不足道的。它允许您定义自己的排序函数,因此您可以根据传递的 2 个对象的任何因素进行排序。
请注意,根据您的示例 json,我必须在整个内容周围添加一组方括号才能使 PHP 解析为 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("\n\n");
// 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("\n\n");
?>