dongpan3001 2018-11-27 08:05
浏览 542
已采纳

根据REST API调用中的多个条件进行排序和筛选

I would like to sort the data I receive from an API call on multiple criteria and afterwards filter it to display the data in 2 different tables.

This is the code that I have now to sort the data, on 1) machine->id, 2) date, 3) ordering.

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");

After that I filter only the data with a specific machine->id to show that data in a table:

$machine1 = array_filter($obj, function($object){
    return ($object->machine->id == 1141);
});

and

$machine2 = array_filter($obj, function($object){
    return ($object->machine->id == 1259);
});

now the data in the table for machine1 looks like this, the date sorting is not working properly:

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

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douzhuo6931 2018-11-27 08:39
    关注

    That happens because you use the machine id in the compare function but after you filter different machines.

    Think of the case:

    1. time = 2015, id = 3, order 2
    2. time = 2016, id = 4, order 3
    3. time = 2013, id = 3, order 3
    

    when comparing 1 and 2 order stays but on 2 and 3 they swap because the ID but the n you will get 1 before 3 which is wrong order for you (dates...)

    I recommend first to split the big array for machine array as you did with:

    $machine1141 = array_filter($obj, function($object){
        return ($object->machine->id == 1141);
    });
    

    Then use compare function as:

    function cmp($a, $b)
    {
        if (strtotime($a->date) == strtotime($b->date))
            return $a->ordering - $b->ordering;
        return strtotime($a->date) - strtotime($b->date);
    }
    usort($machine1141, "cmp");
    

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)