我有一个阵列
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
我想通过状态desc顺序重新排列这个数组我尝试
usort($usersdetailsA, function($a, $b) {
return $a->status <=> $b->status;
});
我得到了一个错误
usort()期望参数1是数组,给定对象
我试过$ usersdetailsA = $ this - > $ usersdetailsA-> getValues();我得到了一个错误
未定义的属性:TCG \ Voyager \ Http \ Controllers \ Users :: $ [{“id”:79,“name”:“shelin”,“status”:0},{“id”:80,“name”:“ shanu”, “状态”: “2”},{ “ID”:81, “名称”: “linto”, “状态”: “2”},{ “ID”:82, “名称”: “约瑟夫” “状态”:0}]
预期产出
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[1] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[2] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
Any help would be appreciated.Thanks in advance
你可以用给定的密钥用collection方法对sort进行排序:
$sorted = $collection->sortByDesc('status');