如何基于两个键求和(子总数)数组?

时间:2016-02-08 12:49:32

标签: php arrays

我的数组具有以下值

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

我希望对数组求和,首先按日期,然后按sku,我想要结果如下

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

我已经探索了堆栈溢出并发现许多帖子根据一个键来计算总和,在这种情况下,我想首先根据日期和sku对数组值求和。 请帮助相同的

2 个答案:

答案 0 :(得分:3)

尝试以下解决方案:

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

<强>输出:

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

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

        )

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

        )

)

答案 1 :(得分:0)

这是另一种解决方案,它会给你一个稍微不同的输出,这可能是也可能不是首选。

$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,
)*/