在PHP中获取多维数组的总和

时间:2014-04-28 19:43:09

标签: php multidimensional-array

我需要获取每个指定月份的数组中所有数值的总和。理想情况下,它将返回以下格式。

四月

总计= 22

三月

总计='总和'

Array
    (
        [April] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 
                [4] => 3
                [5] => 
                [6] => 2
                [7] => 6
                [8] => 3
                [9] => 2
            )

        [March] => Array
            (
                [0] => 3.19198
                [1] => 2.52219
                [2] => 3.40053
                [3] => 2.42639
                [4] => 3.92301
                [5] => 3.23758
                [6] => 3.22457
                [7] => 2.62855
            )

1 个答案:

答案 0 :(得分:1)

使用array_sum()在每个子阵列上应用array_map()

$result = array_map('array_sum', $data);

输出:

Array
(
    [April] => 22
    [March] => 24.5548
)

Demo