如何获取每个子数组的每列的数组总和?

时间:2015-04-29 11:45:14

标签: php arrays for-loop foreach sum

这是我的阵列:

Array ( [HRI] => Array ( [0] => Array ( [0] => 14157.72 [1] => 10157.72 ) [1] => Array ( [0] => 39140.94 [1] => 39140.94 ) [2] => Array ( [0] => 36383.66 [1] => 36383.66 ) [3] => Array ( [0] => 38508.00 [1] => 38508.00 ) [4] => Array ( [0] => 8424.00 [1] => 8424.00 ) ) [MPU] => Array ( [0] => Array ( [0] => 432.00 [1] => 1200.00 ) ) [RHA] => Array ( [0] => Array ( [0] => 111562.50 [1] => 94237.50 ) [1] => Array ( [0] => 37880.00 [1] => 31400.00 ) [2] => Array ( [0] => 11364.00 [1] => 9420.00 ) [3] => Array ( [0] => 23719.50 [1] => 18832.50 ) [4] => Array ( [0] => 26705.00 [1] => 21800.00 ) [5] => Array ( [0] => 5190.00 [1] => 3816.00 ) [6] => Array ( [0] => 8707.50 [1] => 6966.00 ) [7] => Array ( [0] => 0.00 [1] => 0.00 ) [8] => Array ( [0] => 0.00 [1] => 0.00 ) [9] => Array ( [0] => 0.00 [1] => 0.00 ) [10] => Array ( [0] => 128952.00 [1] => 94500.00 ) [11] => Array ( [0] => 0.00 [1] => 0.00 ) ) [PBA] => Array ( [0] => Array ( [0] => 58816.26 [1] => 50784.36 ) ) )

我想浏览每个subArray并获取每列的总和。

E.g。

对于键为HRI的子数组:

[HRI] => 
    Array (
        [0] => Array ( [0] => 14157.72 [1] => 10157.72 )
        [1] => Array ( [0] => 39140.94 [1] => 39140.94 )
        [2] => Array ( [0] => 36383.66 [1] => 36383.66 )
        [3] => Array ( [0] => 38508.00 [1] => 38508.00 )
        [4] => Array ( [0] => 8424.00 [1] => 8424.00 )
    )

预期产出:

column 0:
  14157.72 + 39140.94 + 36383.66 + 38508.00 + 8424.00 = ?

column 1:
  10157.72 + 39140.94 + 36383.66 + 38508.00 + 8424.00 = ?

我目前的代码:

$amount = [];
for($o=0;$o<count($totalamount);$o++){
    print_r($totalamount[$o]['login']); 

    if($totalamount[$o]['login'] == $totalamount[$o]['login']){
        //$amount[$totalamount[$o]['login']] += $totalamount[$o]['piamount'];
        $piamount[$totalamount[$o]['login']][] = array_sum(array($totalamount[$o]['piamount'].','));
    }
}

如何更改代码以获得预期结果?

2 个答案:

答案 0 :(得分:1)

这应该适合你:

只需循环遍历数组,并使用array_sum()

获取每个子数组的总和
foreach($arr as $k => $v)
    echo $k . " = " . array_sum($v) . "<br>";

示例输入/输出:

$arr = [
        [1,2,3],
        [4,5,6],
        [7,8,9],
    ];

输出:

0 = 6
1 = 15
2 = 24

修改

由于您更新了数组结构,只需使用:

foreach($arr as $k => $v)
    echo $k . " = " . array_sum(array_map("array_sum", $v)) . "<br>";

编辑2:

如果你想要每个子数组数组的总和,你必须使用两个foreach循环,如下所示:

foreach($arr as $k1 => $innerArray) {
    foreach($innerArray as $k2 => $v)
        echo "$k1-$k2 = " . array_sum($v) . "<br>";
    echo "<br><br>";
}

编辑3:

我想我终于看到了你想要的东西:你只想浏览每个子数组并得到子数组每列的总和:

foreach($arr as $k1 => $innerArray) {
    foreach($innerArray[0] as $k2 => $v)
        echo "$k1-$k2 = " . array_sum(array_column($innerArray, $k2)) . "<br>";
    echo "<br><br>";
}

答案 1 :(得分:1)

$a=Array (
    'HRI'=> Array (14157.72, 39140.94 , 36383.66,38508, 8424 ),
    'RHA'=> Array (111562.5, 37880 ,  11364 ,23719.5,  26705 ),
    'PBA'=> Array (58816.26),
    'MPU'=> Array (432 )
);
$sumHRI=0;

foreach ($a['HRI'] as $key=>$value) $sumHRI+=$value;

echo "HRI=$sumHRI";

更新问题:

  $a=[
    'HRI'=>[
        [14157.72, 39140.94 , 36383.66,38508, 8424],
        [14157.72, 39140.94 , 36383.66,38508, 8424],
        [14157.72, 39140.94 , 36383.66,38508, 8424],
        [14157.72, 39140.94 , 36383.66,38508, 8424],
        ],
    'RHA'=>[111562.5, 37880 ,  11364 ,23719.5,  26705 ],
    'PBA'=> [58816.26],
    'MPU'=> [432]
];

$sumArray=[];
foreach ($a as $k=>$bigArray){
    $sumArray[$k]=0;
    foreach($bigArray as $subArray) $sumArray[$k]+=array_sum($subArray);
    echo "$k={$sumArray[$k]}";
}

如果我理解正确,你想要计算的总和。如果你想实际打印数字和符号+,必须在某处添加字符串连接。