TuffRivers's avatar

Iterate over multi-dimensional array to find duplicates?

I have a multi-dimensional array that i want to loop through and find any key/value duplicates. If there is a duplicate i want to add the quantities together and then remove one of them from my array (array_filter).

For example, this is my Array of Arrays. If there is a duplicate sku => i want to add their quantities together and then remove one from the array based on the ID (it will always be unique).

DEP1030BLKCO - this sku is duplicated twice. So i would like to add their quantities together (8+8) and remove one of their keys so its a single DEP1030BLKCO sku with quantity of 16.

I know that i can initialize an array variable $duplicates to store IDs of duplicates and then use an array filter function to filter out any IDs added to the array (essentially removing duplicates) but how can i find the duplicates? I saw array_unique but im unsure if that will help in this situation.

Is there a better approach than *this?

Array (   

  [DEP2000RODSIDL2] => Array
        (
            [product_id] => 167
            [sku] => DEP2000RODSIDL2
            [name] => Side Rod - Large
            [weight] => 1.00
            [quantity] => 32
            [product_options] => 
            [id] => DEPRODSIDL
        )

    [1DEP1030BLKL] => Array
        (
            [product_id] => 173
            [sku] => DEP30BLKL
            [name] => Large Black
            [weight] => 12
            [quantity] => 1
            [product_options] => 
            [id] => 1DEP30BLKL
        )

    [1DEP1030BLKM] => Array
        (
            [product_id] => 168
            [sku] => DEP30BLKCO
            [name] => Black Carry
            [weight] => 8
            [quantity] => 1
            [product_options] => 
            [id] => 8952
        )

    [1DEP1030BLKCO] => Array
        (
            [product_id] => 168
            [sku] => DEP30BLKCO
            [name] => Black Carry
            [weight] => 8
            [quantity] => 1
            [product_options] => 
            [id] => 1DEP30BLKCO
        )
)

0 likes
6 replies
realrandyallen's avatar

Try this:

$reduced = $data->groupBy('sku')->map(function ($items) {
    $item = $items->first();
    $item['quantity'] = $items->sum('quantity');
        
    return $item;
});
TuffRivers's avatar

@realrandyallen unfortunately i am not using Laravel, this is core PHP, or else i feel like your bit of code would be super effective!

TuffRivers's avatar

So im getting close

$result = array();
        foreach ($newArray as $singleSku){
            $result[$singleSku['sku']][] = $singleSku;
         
        }

This foreach here will loop through the array and "bring" duplicates into the same array, based on SKU.

How can i then add the quantitys together? this is the result it out puts? I dont like how it brings the array one level deeper either, this seems to be getting messy


[DEP30BLKCO] => Array
        (
            [0] => Array
                (
                    [id] => 8952
                    [order_id] => 5815
                    [product_id] => 168
                    [name] => Black Carry
                    [sku] => DEP30BLKCO
                    [weight] => 8.0000
                    [width] => 22.0000
                    [height] => 14.0000
                    [depth] => 9.0000
                    [quantity] => 1

                )

            [1] => Array
                (
                    [product_id] => 168
                    [sku] => DEP30BLKCO
                    [name] => Black Carry
                    [weight] => 8
                    [quantity] => 1
                    [product_options] => 
                    [id] => 1DEP30BLKCO
                )

        )

douglasakula's avatar

Something along this lines

$final = [];
    array_walk_recursive($input, function($item, $key) use (&$final){
        $final[$key] = isset($final[$key]) ?  $item + $final[$key] : $item;
 });

douglasakula's avatar

or


$value = array_sum(array_column($your_input_array, $key)) 

Please or to participate in this conversation.