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

根据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?

图片转代码服务由CSDN问答提供 功能建议

我想根据多个条件对从API调用中收到的数据进行排序,然后对其进行过滤以显示数据 在2个不同的表中。

这是我现在要对数据进行排序的代码,1)machine-> id,2)date,3)order。

  function cmp($ a,$ b)
 {
 if if(strtotime($ a-> date)== strtotime($ b-> date)){
 返回$ a->订购 -  $ b->订购; 
} 
 if($ a-> machine-> id == $ b-> machine-> id){
返回strtotime  ($ a-> date) -  strtotime($ b-> date); 
} 
返回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,fun  ction($ object){
 return($ object-> machine-> id == 1259); 
}); 
   
 
 

现在的数据 在machine1的表中看起来像这样,日期排序不能正常工作:

  2018-11-26T23:00:00Z  - 订购:1 
2018-11-26T23  :00:00Z  - 订购:3 
2018-11-27T23:00:00Z  - 订购:2 
2018-11-27T23:00:00Z  - 订购:3 
2018-11-27T23:00:00Z  - 订购:4  
2018-11-27T23:00:00Z  - 订购:5 
2018-11-25T23:00:00Z  - 订购:1 
2018-11-25T23:00:00Z  - 订购:2 
2018-11-26T23:00  :00Z  - 订购:2 
2018-11-27T23:00:00Z  - 订购:1 
2018-11-25T23:00:00Z  - 订购:3 
2018-11-25T23:00:00Z  - 订购:4 
2018  -11-25T23:00:00Z  - 订购:5 
2018-11-25T23:00:00Z  - 订购:6 
2018-11-25T23:00:00Z  - 订购:7 
2018-11-25T23:00:00Z  - 订购:8 
2018-11-25T23:00:00Z  - 订购:9 
2018-11-25T23:00:00Z  - 订购:10 
2018-11-25T23:00:00Z  - 订购:11 
2018-11  -26T23:00:00Z  - 订购:4 
2018-11-26T23:00:00Z  - 订购:5 
2018-11-26T23:00:00Z  - 订购:6  
2018-11-26T23:00:00Z  - 订购:7 
2018-11-26T23:00:00Z  - 订购:8 
2018-11-26T23:00:00Z  - 订购:9 
2018-11-26T23:00  :00Z  - 订购:10 
2018-11-26T23:00:00Z  - 订购:11 
2018-11-26T23:00:00Z  - 订购:12 
2018-11-26T23:00:00Z  - 订购:13 
2018  -11-26T23:00:00Z  - 订购:14 
2018-11-26T23:00:00Z  - 订购:15 
2018-11-26T23:00:00Z  - 订购:16 
    
 
 

我做错了什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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!

    点赞 打赏 评论

相关推荐 更多相似问题