uasort with< => (宇宙飞船运营商)

时间:2017-02-16 10:10:13

标签: php

如何使用<=>(宇宙飞船运营商)重写此uasort($hits, function($a, $b) { if($a['total'] == $b['total']) { if($a['vat'] == $b['vat']) { return 0; } else { return ($a['vat'] > $b['vat']) ? -1 : 1; } } else { return ($a['total'] > $b['total']) ? -1 : 1; } } 功能?

FlatFileItemReader

3 个答案:

答案 0 :(得分:3)

return $b['total'] <=> $a['total'] ?: $b['vat'] <=> $a['vat'];

如果总数相等,<=>会返回0,这是假的,因此?:运算符将返回大桶比较的结果。将返回第一个非0结果。

答案 1 :(得分:3)

这样的东西
uasort($hits, function($a, $b){
    return [$b['total'], $b['vat']] <=> [$a['total'], $a['vat']];
});

答案 2 :(得分:0)

那应该有用

uasort($hits, function($a, $b){
    if($a['total'] == $b['total']){
        return ($b['vat'] <=> $a['vat']);
    }
    else{
        return ($b['total'] <=> $a['total']);
    }
});