php:没有foreach的数组和

时间:2017-11-02 13:12:21

标签: php arrays

我有一个像这样的数组

Array
(
    [1_DAY_2017] => Array
        (
            [SAMSUNG] => Array
                (
                    [0] => 549
                    [1] => 199
                    [2] => 999
                )

            [XIAOMI] => Array
                (
                    [0] => 199
                    [1] => 2999
                    [2] => 499
                )

        )

    [2_DAY_2017] => Array
        (
            [SAMSUNG] => Array
                (
                    [0] => 699
                    [1] => 999
                )

            [LENOVO] => Array
                (
                    [0] => 280
                    [1] => 2550
                    [2] => 849
                )

        )

    [3_DAY_2017] => Array
        (
            [OPPO] => Array
                (
                    [0] => 500
                    [1] => 599
                )

            [SAMSUNG] => Array
                (
                    [0] => 799
                )

        )

    [4_DAY_2017] => Array
        (
            [SAMSUNG] => Array
                (
                    [0] => 1299
                    [1] => 499
                    [2] => 799
                    [3] => 2500
                )

            [OPPO] => Array
                (
                    [0] => 299
                    [1] => 349
                    [2] => 499
                )

        )

    [5_DAY_2017] => Array
        (
            [XIAOMI] => Array
                (
                    [0] => 500
                    [1] => 270
                    [2] => 340
                )

            [VIVO] => Array
                (
                    [0] => 4599
                    [1] => 299
                )

        )

    [6_DAY_2017] => Array
        (
            [VIVO] => Array
                (
                    [0] => 240
                    [1] => 1899
                    [2] => 759
                    [3] => 530
                )

            [OPPO] => Array
                (
                    [0] => 999
                )

        )

    [7_DAY_2017] => Array
        (
            [OPPO] => Array
                (
                    [0] => 300
                    [1] => 252
                    [2] => 1290
                )

            [LENOVO] => Array
                (
                    [0] => 570
                    [1] => 1300
                    [2] => 666
                )

        )

从此我想得到一个数组

$output= [SAMSUNG => 9341, XIAOMI => 4807]

此处每个项目都包含嵌套数组中项目的总和。

目前我的解决方案每个循环都包含2个以上但是有没有办法优化这个?

3 个答案:

答案 0 :(得分:2)

您可以使用array_sum,array_map和array_column函数来获取结果

array_sum(array_map('array_sum', array_column($a, 'SAMSUNG')))

答案 1 :(得分:1)

使用单array_reduce个功能:

// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
    if (isset($v['SAMSUNG'])) $r['SAMSUNG'] += array_sum($v['SAMSUNG']);
    if (isset($v['XIAOMI'])) $r['XIAOMI'] += array_sum($v['XIAOMI']);
    return $r;
}, ['SAMSUNG' => 0, 'XIAOMI' => 0]);

答案 2 :(得分:0)

$sumArray = array();

foreach ($YourArray as $k=>$subArray) {
  foreach ($subArray as $id=>$value) {
    $sumArray[$id]+=$value;
  }
}

print_r($sumArray);