我有一个数组
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
)
)
)
我想按状态降序重新排列该数组
我尝试
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", "status":"2"},{"id":81,"name":"linto","status":"2"},{"id":82,"name":"joseph","status “: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
您可以排序一个收藏 https://laravel.com/docs/5.6/collections#method-sortby with sort https://laravel.com/docs/5.6/collections#method-sortbydesc给定键的方法:
$sorted = $collection->sortByDesc('status');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)