phparraysmultidimensional-arraysumgrouping

Group rows of a 2d array by column and sum another column in each group


Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.

This is my array:

Array
(
[0] => Array
    (
        [sku] => '82368-21'
        [size] => 36
        [quantity] => 1
    )

[1] => Array
    (
        [sku] => '82368-21'
        [size] => 36
        [quantity] => 3
    )

[2] => Array
    (
        [sku] => '82368-22'
        [size] => 38
        [quantity] => 0
    )

[3] => Array
    (
        [sku] => '82368-23'
        [size] => 39
        [quantity] => 2
    )

[4] => Array
    (
        [sku] => '82368-23'
        [size] => 39
        [quantity] => 1
    )
)

As you can see the shoes has multiple sizes and its quantity. There is no need to remove any duplicates as they should all group by size. I would like to output the following:

 Array
(
[0] => Array
    (
        [sku] => '82368-21'
        [size] => 36
        [quantity] => 4
    )


[1] => Array
    (
        [sku] => '82368-22'
        [size] => 38
        [quantity] => 0
    )

[2] => Array
    (
        [sku] => '82368-23'
        [size] => 39
        [quantity] => 3
    )
)

Solution

  • You can do this with a simple foreach loop:

     $array = array (
        0 => 
        array (
            'size' => 36,
            'quantity' => 1
        ),
        1 => 
        array (
            'size' => 36,
            'quantity' => 3
        ),
        2 => 
        array (
            'size' => 38,
            'quantity' => 0
        ),
        3 => 
        array (
            'size' => 39,
            'quantity' => 2
        ),
        4 => 
        array (
            'size' => 39,
            'quantity' => 1
        )
    );
    
    $out = [];
    
    foreach($array as $value){
    
        $key = $value['size'];
        if(!isset($out[$key])){
            $out[$key] = $value;
        }else{
            $out[$key]['quantity'] += $value['quantity'];
        }
    }
    
    print_r($out);
    

    Output

    Array
    (
        [36] => Array
            (
                [size] => 36
                [quantity] => 4
            )
    
        [38] => Array
            (
                [size] => 38
                [quantity] => 0
            )
    
        [39] => Array
            (
                [size] => 39
                [quantity] => 3
            )
    
    )
    

    Sandbox

    I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.

     //reset the array keys
     $out = array_values($out);
     print_r($out);
    

    Output

    Array
    (
        [0] => Array
            (
                [size] => 36
                [quantity] => 4
            )
    
        [1] => Array
            (
                [size] => 38
                [quantity] => 0
            )
    
        [2] => Array
            (
                [size] => 39
                [quantity] => 3
            )
    
    )
    

    Sandbox

    Cheers