在Laravel中对一个多维数组进行排序

问题描述 投票:0回答:1

我使用的是laravel,想根据['messages']"update_at "字段对这个数组进行排序。该数组应该在顶部显示最新的消息。array_multisort( array_column($yourArray, "price"), SORT_ASC, $yourArray ) 但没有成功。

这是我的多维数组,我想按日期排序。

Array
(
    [current_page] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 236
                    [user] => Array
                        (
                            [id] => 91
                            [email] => [email protected]
                            [updated_at] => 2020-04-29 09:28:26
                        )

                    [messages] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [id] => 235
                    [description] => demo3
                    [user] => Array
                        (
                            [id] => 91
                            [email] => [email protected]
                            [updated_at] => 2020-04-29 09:28:26
                        )
                    [messages] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 95
                                    [message] => Pickup message test
                                    [user_id] => 35
                                    [created_at] => 2020-04-28 12:28:18
                                    [updated_at] => 2020-04-28 12:28:18
                                )
                        )

                )

            [2] => Array
                (
                    [id] => 215
                    [description] => Testing for messages
                    [user] => Array
                        (
                            [id] => 35
                            [email] => [email protected]
                            [updated_at] => 2020-01-26 17:34:35
                        )

                    [messages] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 88
                                    [message] => Testing for the message if not sent by business first.
                                    [user_id] => 35
                                    [created_at] => 2020-04-28 12:15:14
                                    [updated_at] => 2020-04-28 12:15:14

                                )

                            [1] => Array
                                (
                                    [id] => 90
                                    [message] => Looks good.
                                    [user_id] => 69
                                    [created_at] => 2020-04-28 12:15:45
                                    [updated_at] => 2020-04-28 12:15:45

                                )


                        )

                )

        )
)

我怎样才能实现这个目标?

php laravel sorting multidimensional-array
1个回答
0
投票

使用usort函数 https:/www.php.netmanualenfunction.usort.php

写一个回调,比较嵌套的create_at属性。

如果你有一个laravel集合,你也可以使用

$collection = $collection->sort(function ($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? -1 : 1;
});

或者如果你使用的是雄辩,你可以从SQL中订购。

Message::where(...)->orderBy('created_at', 'DESC')->paginate(20)

从用户关系看

$user->with(['messages' => function ($query) {
    $query->orderBy('created_at', 'DESC');
}]);
© www.soinside.com 2019 - 2024. All rights reserved.