标题说明了一切:在 php 中是否有一个垂直求和函数,它在整个数组上添加一个(子)数组值,如下所示:
// pseudo code that would return the sum of "income" for all days of the year
// for example
vertical_sum($array[$day_of_year]["income"]);
最佳答案
原生函数?不完全是,但数组缩减可以提供帮助:
$array = array(
array('income' => 1), //day 1
array('income' => 3), //day 2, etc
array('income' => 6),
array('income' => 7)
);
echo array_reduce($array, function($curr_total, $this_val) {
return $curr_total + $this_val['income'];
}, 0); //17
关于php - "vertical"数组子元素总和的 native 函数,如 vertical_sum($array[$day_of_year] ["income"]);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11760903/