我有一组 json 对象,我想对它进行升序排序,并将空值放在最后。以下是我尝试过的代码。两个 uasort() 单独工作都非常好,但是当我一个接一个地放置它们时,它只是根据最新的函数对数组进行排序。如何按升序对数组进行排序,并将空值放在该升序排序列表的末尾?uasort($arr, function($a,$b) { return $a->score > $b->score ? 1 : -1; });uasort($arr, function($a) { return ( is_null($a->score==NULL) OR $a->score == "") ? 1 : -1;});
1 回答

德玛西亚99
TA贡献1770条经验 获得超3个赞
使用一个测试两种条件的比较函数。
uasort($arr, function($a, $b) {
if ($a->score === $b->score) {
return 0;
}
if ($a->score === NULL || $a->score === "") {
return 1;
}
if ($b->score === NULL || $b->score === "") {
return -1;
}
return $a->score > $b->score ? 1 : -1;
}
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报
0/150
提交
取消