usort not working for laravel multidimensional arr

2019-07-23 19:54发布

I have an array

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
                )

        )

)

I want to rearrange this array by status desc order I try

usort($usersdetailsA, function($a, $b) {
    return $a->status <=> $b->status;
});

I got an error like

usort() expects parameter 1 to be array, object given

I tried $usersdetailsA = $this->$usersdetailsA->getValues(); the i got an error like

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}]

Expected output

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

1条回答
来,给爷笑一个
2楼-- · 2019-07-23 20:19

You can sort a collection with sort methods by a given key:

$sorted = $collection->sortByDesc('status');
查看更多
登录 后发表回答