Multiplying array indexes in PHP with array_reduce
为什么加和乘时
1 2 3 4 5 6 7 8 9 10 | function sum($arr){ print_r(array_reduce($arr, function($a, $b){return $a + $b;})); } function multiply($arr){ print_r(array_reduce($arr, function($a, $b){return $a * $b;})); } sum(array(1, 2, 3, 4, 5)); // 15 multiply(array(1, 2, 3, 4, 5)); // 0 |
根据文档,您可能想尝试
1 2 3 |
以下是此讨论的引文:
The first parameter to the callback is an accumulator where the result-in-progress is effectively assembled. If you supply an $initial value the accumulator starts out with that value, otherwise it starts out null.