usort不适用于laravel多维数组 [英] usort not working for laravel multidimensional arrays
问题描述
我有一个数组
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顺序重新排列此数组 我尝试
I want to rearrange this array by status desc order I try
usort($usersdetailsA, function($a, $b) {
return $a->status <=> $b->status;
});
我遇到了类似的错误
usort()期望参数1为数组,给定对象
usort() expects parameter 1 to be array, object given
我尝试过 $ usersdetailsA = $ this-> $ usersdetailsA-> getValues(); 我遇到了类似的错误
I tried $usersdetailsA = $this->$usersdetailsA->getValues(); the i got an error like
未定义的属性: 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}]
Undefined property: 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
推荐答案
您可以对 sort 方法进行>收藏:>
You can sort a collection with sort methods by a given key:
$sorted = $collection->sortByDesc('status');
这篇关于usort不适用于laravel多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!