Search code examples
phplaravelcollectionsduplicatescollect

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


when i use

$data_collect->groupBy('group_id')

and give me result like this:

   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
                                    )
                            )
                    )
            ) )

But I want to filter out duplicate data to keep the lower "price" values and remove the higher "price" values. So result will be like this:

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
                                )
                        )
                )
        )
)

Do you know how to do it? Thank you for your help.


Solution

  • I guess that you don't need groups in resulted collection if you need only first elemnt from each group.

    $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.
        });