【问题标题】:Sort a multidimensional array in Laravel在 Laravel 中对多维数组进行排序
【发布时间】:2020-08-20 10:13:06
【问题描述】:

我正在使用 laravel 并希望根据 ['messages'] "updated_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] => demo3@test.com
                            [updated_at] => 2020-04-29 09:28:26
                        )

                    [messages] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [id] => 235
                    [description] => demo3
                    [user] => Array
                        (
                            [id] => 91
                            [email] => demo3@test.com
                            [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] => test@outlook.com
                            [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】:

    使用usort函数https://www.php.net/manual/en/function.usort.php

    写一个回调比较嵌套的 created_at 属性

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

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

    或者如果你使用 eloquent,你可以从 SQL 中订购

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

    来自用户关系

    $user->with(['messages' => function ($query) {
        $query->orderBy('created_at', 'DESC');
    }]);
    

    【讨论】:

    • 我正在从这个$claims = Claim::query(); $claims = $claims-&gt;where('status', $status); $claims-&gt;load('user', 'item', 'item.category', 'actions', 'actions.user', 'messages', 'messages.user'); 获取数据我如何使用这个$user-&gt;with(['messages' =&gt; function ($query) { $query-&gt;orderBy('created_at', 'DESC'); }]);
    • 您需要只订购消息还是订购所有收藏?
    • 我需要订购所有收藏
    猜你喜欢
    • 2017-08-27
    • 2010-10-13
    • 1970-01-01
    • 2017-11-09
    • 2023-03-23
    • 2013-03-11
    • 1970-01-01
    相关资源
    最近更新 更多