Manoj February 2016

How to sum (sub total) array based on two keys?

I have array with below values

 Array
(
    [0] => Array
        (
            [order_date] => 2016-01-01
            [sku] => RK101
            [qty] => 2
        )

    [1] => Array
        (
            [order_date] => 2016-01-01
            [sku] => RK101
            [qty] => 5
        )

    [2] => Array
        (
            [order_date] => 2016-01-01
            [sku] => RK102
            [qty] => 4
        )

    [3] => Array
        (
            [order_date] => 2016-01-02
            [sku] => RK101
            [qty] => 2
        )

    [4] => Array
        (
            [order_date] => 2016-01-02
            [sku] => RK101
            [qty] => 2
        )
)

and i want to sum the array, first by date and then by sku, i want result like below

    Array
(
    [2016-01-01] => Array
    (
        [RK101] => Array
        (
            [qty] => 7
        )
        [RK102] => Array
        (
            [qty] => 4
        )
    )
    [2016-01-01] => Array
    (
        [RK101] => Array
        (
            [qty] => 4
        )
    )
)

i have explore stack overflow and found many post that calculates the sum based on one key, by in this case i want to sum the array values based on date first and then by sku. please help with the same

Answers


Chetan Ameta February 2016

try below solution:

<?php
echo '<pre>';

$array = Array
(
    '0' => Array
    (
        'order_date' => '2016-01-01',
        'sku' => 'RK101',
        'qty' => 2
    ),

    '1' => Array
    (
        'order_date' => '2016-01-01',
        'sku' => 'RK101',
        'qty' => 5
    ),

    '2' => Array
    (
        'order_date' => '2016-01-01',
        'sku' => 'RK102',
        'qty' => 4
    ),

    '3' => Array
    (
        'order_date' => '2016-01-02',
        'sku' => 'RK101',
        'qty' => 2
    ),

    '4' => Array
    (
        'order_date' => '2016-01-02',
        'sku' => 'RK101',
        'qty' => 2
    )
);

$new_array = array();
foreach ($array as $a) {
    if (!isset($new_array[$a['order_date']][$a['sku']]['qty'])) {
        $new_array[$a['order_date']][$a['sku']]['qty'] = 0;
    }
    $new_array[$a['order_date']][$a['sku']]['qty'] += $a['qty'];
}

print_r($new_array);

output:

Array
(
    [2016-01-01] => Array
        (
            [RK101] => Array
                (
                    [qty] => 7
                )

            [RK102] => Array
                (
                    [qty] => 4
                )

        )

    [2016-01-02] => Array
        (
            [RK101] => Array
                (
                    [qty] => 4
                )

        )

)


Tom Wright February 2016

Here is another solution that gives you a slightly different output, which may or may not be preferred.

$results = array();

$myNums = array(
    array(
        'order_date' => '2016-01-01',
        'sku' => 'RK101',
        'qty' => 2,
    ),
    array(
        'order_date' => '2016-01-01',
        'sku' => 'RK101',
        'qty' => 1,
    ),
    array(
        'order_date' => '2016-01-02',
        'sku' => 'RK101',
        'qty' => 2,
    ),
);

foreach ($myNums as $num) {
    $key = "{$num['order_date']}|{$num['sku']}";
    if (! array_key_exists($key, $results)) {
        $results[$key] = 0;
    }
    $results[$key] += $num['qty'];
}

var_dump($results);
/*
var_dump gives the following...
 Array
(
    [2016-01-01|RK101] => 3,
    [2016-01-02|RK101] => 2,
)*/

Post Status

Asked in February 2016
Viewed 3,230 times
Voted 10
Answered 2 times

Search




Leave an answer