如果你想计算多维数组的总和
<?php
function array_multisum(array $arr): float {
$sum = array_sum($arr);
foreach($arr as $child) {
$sum += is_array($child) ? array_multisum($child) : 0;
}
return $sum;
}
?>
示例
<?php
$data =
[
'a' => 5,
'b' =>
[
'c' => 7,
'd' => 3
],
'e' => 4,
'f' =>
[
'g' => 6,
'h' =>
[
'i' => 1,
'j' => 2
]
]
];
echo array_multisum($data);
//输出: 28
?>