【问题标题】:Laravel filter and sort a collection based on conditionsLaravel 根据条件过滤和排序集合
【发布时间】:2020-07-05 01:35:32
【问题描述】:

例如,我有一个包含以下详细信息的集合:

collect([
                        [
                            'id'       => 1,
                            'bankName' => 'Dare-Schmitt',
                            'pmt'      => 47068.084449396,
                            'period'   => 240
                        ],
                        [
                            'id'       => 2,
                            'bankName' => 'Dare-Schmitt',
                            'pmt'      => 45989.430695784,
                            'period'   => 180
                        ],
                        [
                            'id'       => 3,
                            'bankName' => 'Dare-Schmitt',
                            'pmt'      => 55459.759785392,
                            'period'   => 240
                        ],
                        [
                            'id'       => 4,
                            'bankName' => 'Lorenzo-Platto',
                            'pmt'      => 37068.084449396,
                            'period'   => 50
                        ]
                    ]
            );

我需要对它们进行排序和过滤,方法是只显示同一银行名称的一条记录,其中 pmt 最高,只需检查周期相同的地方。

所以基于过滤和排序后的示例,我必须得到这个结果:

        collect([
                    [
                        'id'       => 2,
                        'bankName' => 'Dare-Schmitt',
                        'pmt'      => 45989.430695784,
                        'period'   => 180
                    ],
                    [
                        'id'       => 3,
                        'bankName' => 'Dare-Schmitt',
                        'pmt'      => 55459.759785392,
                        'period'   => 240
                    ],
                    [
                        'id'       => 4,
                        'bankName' => 'Lorenzo-Platto',
                        'pmt'      => 37068.084449396,
                        'period'   => 50
                    ]
                ]
        );

知道如何实现这一目标吗?我输了一点:D

【问题讨论】:

    标签: laravel sorting collections filtering


    【解决方案1】:

    使用集合method-groupBy在组中创建具有相同backName和相同period的记录。

    并使用map获得最高的pmt记录:

    $data->groupBy(function ($g) {
           return $g['bankName'].' '.$g['period'];
         })->map(function($items) { 
            return $items->sortByDesc('pmt')->first(); // get the highest pmt record
         })->values();
    

    【讨论】:

    • 您好,首先感谢您的帮助! :) 如果我在上面的示例集合中尝试这个解决方案,那么几乎没有问题,180 周期的 Dare-Schmitt 银行也会从结果列表中掉出来,我也需要它。
    • @Wathfea 所以你想获得同一银行名称和同一时期的最高 pmr 吗?我已经更新了答案。
    • 几乎该集合具有其他属性所以我需要根据用户输入(期间)例如 240 银行及其所有详细信息与同一期间之间的最高 pmt。示例:1, Name Test, pmt 1, period 240, color red etc 2, Name Test, pmt 2, period 240, color green etc 3, Name Test, pmt 1, period 180, color blue etc 4, Name Foo, pmt 1, period 60, color red etc 过滤后1, Name Test, pmt 2, period 240, color green etc 2, Name Test, pmt 1, period 180, color blue etc 3, Name Foo, pmt 1, period 60, color red etc 过滤掉第一组
    • 对不起,我的错。它工作正常! :) 非常感谢
    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 2017-06-03
    • 1970-01-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-05-27
    • 2018-05-07
    相关资源
    最近更新 更多