Voting

: six plus three?
(Example: nine)

The Note You're Voting On

Altreus
11 years ago
You can effectively ignore the fact $result is passed into the callback by reference. Only the return value of the callback is accounted for.

<?php

$arr
= [1,2,3,4];

var_dump(array_reduce(
$arr,
function(&
$res, $a) { $res += $a; },
0
));

# NULL

?>

<?php

$arr
= [1,2,3,4];

var_dump(array_reduce(
$arr,
function(
$res, $a) { return $res + $a; },
0
));

# int(10)
?>

Be warned, though, that you *can* accidentally change $res if it's not a simple scalar value, so despite the examples I'd recommend not writing to it at all.

<< Back to user notes page

To Top