【发布时间】:2019-04-28 22:18:27
【问题描述】:
我想根据多个条件对从 API 调用收到的数据进行排序,然后对其进行过滤以在 2 个不同的表中显示数据。
这是我现在对数据进行排序的代码,1) 机器->id,2) 日期,3) 排序。
function cmp($a, $b)
{
if (strtotime($a->date) == strtotime($b->date)){
return $a->ordering - $b->ordering;
}
if ($a->machine->id == $b->machine->id) {
return strtotime($a->date) - strtotime($b->date);
}
return strcmp($a->machine->id, $b->machine->id);
}
usort($obj, "cmp");
之后,我只过滤具有特定机器->id 的数据以在表中显示该数据:
$machine1 = array_filter($obj, function($object){
return ($object->machine->id == 1141);
});
和
$machine2 = array_filter($obj, function($object){
return ($object->machine->id == 1259);
});
现在 machine1 的表中的数据如下所示,日期排序不正常:
2018-11-26T23:00:00Z - ordering: 1
2018-11-26T23:00:00Z - ordering: 3
2018-11-27T23:00:00Z - ordering: 2
2018-11-27T23:00:00Z - ordering: 3
2018-11-27T23:00:00Z - ordering: 4
2018-11-27T23:00:00Z - ordering: 5
2018-11-25T23:00:00Z - ordering: 1
2018-11-25T23:00:00Z - ordering: 2
2018-11-26T23:00:00Z - ordering: 2
2018-11-27T23:00:00Z - ordering: 1
2018-11-25T23:00:00Z - ordering: 3
2018-11-25T23:00:00Z - ordering: 4
2018-11-25T23:00:00Z - ordering: 5
2018-11-25T23:00:00Z - ordering: 6
2018-11-25T23:00:00Z - ordering: 7
2018-11-25T23:00:00Z - ordering: 8
2018-11-25T23:00:00Z - ordering: 9
2018-11-25T23:00:00Z - ordering: 10
2018-11-25T23:00:00Z - ordering: 11
2018-11-26T23:00:00Z - ordering: 4
2018-11-26T23:00:00Z - ordering: 5
2018-11-26T23:00:00Z - ordering: 6
2018-11-26T23:00:00Z - ordering: 7
2018-11-26T23:00:00Z - ordering: 8
2018-11-26T23:00:00Z - ordering: 9
2018-11-26T23:00:00Z - ordering: 10
2018-11-26T23:00:00Z - ordering: 11
2018-11-26T23:00:00Z - ordering: 12
2018-11-26T23:00:00Z - ordering: 13
2018-11-26T23:00:00Z - ordering: 14
2018-11-26T23:00:00Z - ordering: 15
2018-11-26T23:00:00Z - ordering: 16
我做错了什么?
【问题讨论】:
标签: php arrays sorting filtering usort