gpt4 book ai didi

php - "vertical"数组子元素总和的 native 函数,如 vertical_sum($array[$day_of_year] ["income"]);

转载 作者:行者123 更新时间:2023-12-05 01:48:27 24 4
gpt4 key购买 nike

标题说明了一切:在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com