Laravel 7:在集合中查找重复项并保留较低的值并删除其他较高的值

Laravel 7: Find duplicates in a collection and keep lower values and remove the others higher values

当我使用

$data_collect->groupBy('group_id')

然后给我这样的结果:

   Illuminate\Support\Collection Object (
        [items:protected] => Array
            (
                [1] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 1
                                        [title] => Item A   
                                        [group_id] => 1
                                        [price] => 4000
                                    )
    
                                [1] => stdClass Object
                                    (
                                        [id] => 22
                                        [title] => Gru gru  
                                        [group_id] => 1
                                        [price] => 3000
                                    )
                            )
                    )
    
                [7] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 2
                                        [title] => Item B   
                                        [group_id] => 7
                                        [price] => 0
                                    )
                            )
                    )
    
                [4] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 11    
                                        [title] => Item X                                               
                                        [group_id] => 4                                   
                                        [price] => 3000   
                                    )
                            )
                    )
    
                [6] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 12
                                        [title] => My Book                                  
                                        [group_id] => 6                                  
                                        [price] => 3000    
                                    )
                            )
                    )
    
                [2] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 13       
                                        [title] => Item 1A
                                        [group_id] => 2                                   
                                        [price] => 3000    
                                    )
    
                                [1] => stdClass Object
                                    (
                                        [id] => 20
                                        [title] => Item 1B
                                        [group_id] => 2
                                        [price] => 3000
                                    )
                            )
                    )
    
                [3] => Illuminate\Support\Collection Object
                    (
                        [items:protected] => Array
                            (
                                [0] => stdClass Object
                                    (
                                        [id] => 14 
                                        [title] => Bla bla                                  
                                        [group_id] => 3
                                        [price] => 0
                                    )
                            )
                    )
            ) )

但我想过滤掉重复数据以保持 lower "price" 值和 remove 更高的“price”值。所以结果将是这样的:

Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [1] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (

                            [1] => stdClass Object
                                (
                                    [id] => 22
                                    [title] => Gru gru  
                                    [group_id] => 1
                                    [price] => 3000
                                )
                        )
                )

            [7] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 2
                                    [title] => Item B   
                                    [group_id] => 7
                                    [price] => 0
                                )
                        )
                )

            [4] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 11    
                                    [title] => Item X                                               
                                    [group_id] => 4                                   
                                    [price] => 3000     
                                )
                        )
                )

            [6] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 12
                                    [title] => My Book                                  
                                    [group_id] => 6                                  
                                    [price] => 3000    
                                )
                        )
                )

            [2] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 13       
                                    [title] => Item 1A
                                    [group_id] => 2                                   
                                    [price] => 3000   
                                )
                )

            [3] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 14 
                                    [title] => Bla bla                                  
                                    [group_id] => 3
                                    [price] => 0
                                )
                        )
                )
        )
)

你知道怎么做吗?感谢您的帮助。

我想如果您只需要每个组中的第一个元素,则不需要结果集合中的组。

$data = $data
    ->groupBy('group_id')
    ->map(function ($group) {
        $sorted = $group->sortBy('price');
        return $sorted->first();
        //or return collect([$sorted->first()]) if you need groups in resulted collection.
    });