Multiplying array indexes in PHP with array_reduce -


why array_reduce() method work differently when adding , multiplying? when add array values below, code produces expected result: 15. when multiply, returns: 0. same code... difference + sign switched * sign.

  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 

according documentation, might wanna try

function multiply($arr){         print_r(array_reduce($arr, function($a, $b){return $a * $b;},1)); } 

here quote this discussion:

the first parameter callback accumulator result-in-progress assembled. if supply $initial value accumulator starts out value, otherwise starts out null.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -