三维数组求和PHP

时间:2015-03-04 15:21:17

标签: php multidimensional-array

我想要这两个数组的总和。

第一阵列:

Array(
[abc] => Array
    (
    [yesterday] => 20
    [today] => 50
    [tomorrow] => 70
    )

[pqr] => Array
    (
    [yesterday] => 10
    [today] => 20
    [tomorrow] => 30
    )

[xyz] => Array
    (
    [yesterday] => 30
    [today] => 20
    [tomorrow] => 10
    )
)

第二阵列:

Array(
[abc] => Array
    (
    [yesterday] => 70
    [today] => 20
    [tomorrow] => 50
    )

[pqr] => Array
    (
    [yesterday] => 20
    [today] => 30
    [tomorrow] => 10
    )

[xyz] => Array
    (
    [yesterday] => 10
    [today] => 30
    [tomorrow] => 20
    )
)

两个数组的总和:

Array(
[abc] => Array
    (
    [yesterday] => 90
    [today] => 70
    [tomorrow] => 120
    )

[pqr] => Array
    (
    [yesterday] => 30
    [today] => 50
    [tomorrow] => 40
    )

[xyz] => Array
    (
    [yesterday] => 40
    [today] => 50
    [tomorrow] => 30
    )
)

3 个答案:

答案 0 :(得分:2)

$b['yesterday'] = array_sum(array_column($a,'yesterday'));
$b['today'] = array_sum(array_column($a,'today'));
...

我希望这能给出这个想法。我知道这段代码不是最终结果。

答案 1 :(得分:1)

这应该适合你:

在这里,我将$arr1$arr2的每个innerArray与array_map()一起浏览。然后我再次遍历innerArray的每个值,并将它们与array_map()一起添加到一起之后,我将返回的数组与innerArray的键与array_combine()组合在一起。最后,我再次将数组与$arr1的键组合在一起。

<?php

    $arr1 = [
            "abc" => [
                "yesterday" => 20,
                "today" => 50,
                "tomorrow" => 70
            ],
            "pqr" => [
                "yesterday" => 10,
                "today" => 20,
                "tomorrow" => 30
            ],
            "xyz" => [
                "yesterday" => 30,
                "today" => 20,
                "tomorrow" => 10
            ]
        ];

    $arr2 = [
            "abc" => [
                "yesterday" => 70,
                "today" => 20,
                "tomorrow" => 50
            ],
            "pqr" => [
                "yesterday" => 20,
                "today" => 30,
                "tomorrow" => 10
            ],
            "xyz" => [
                "yesterday" => 10,
                "today" => 30,
                "tomorrow" => 20
            ]
        ];  


    $result = array_combine(array_keys($arr1), array_map(function($a1, $a2){
        return array_combine(array_keys($a1), array_map(function($v1, $v2){ return $v1+$v2;}, $a1, $a2));
    }, $arr1, $arr2));


    print_r($result);


?>

输出:

Array
(
    [abc] => Array
        (
            [yesterday] => 90
            [today] => 70
            [tomorrow] => 120
        )

    [pqr] => Array
        (
            [yesterday] => 30
            [today] => 50
            [tomorrow] => 40
        )

    [xyz] => Array
        (
            [yesterday] => 40
            [today] => 50
            [tomorrow] => 30
        )

)

答案 2 :(得分:1)

$output = array();
foreach ($array1 as $index => $value) {

    $output[$index]['yesterday'] = ($array2[$index]['yesterday'] + $value['yesterday']);
    $output[$index]['today'] = ($array2[$index]['today'] + $value['today']);
    $output[$index]['tomorrow'] = ($array2[$index]['tomorrow'] + $value['tomorrow']);
}

这可能不是最漂亮的解决方案,但我认为通过这个和@ Rizier123的解决方案,您可以将所有内容包装在一个函数中并一次传递2个数组,类似于以下函数blueprint {{1而不是使用function sumArrays($array1, $array2, &output)使用=

希望这有帮助!