note 114491 added to function.array-reduce
| From: | Altreus | Date: | Wed, 26 Feb 2014 14:30:24 +0000 |
| Subject: | note 114491 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198733@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 87.124.126.49
Probable Submitter: 31.24.0.198
----
Manual Page -- http://php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/114491
Del: integrated -- https://master.php.net/note/delete/114491/integrated
Del: useless -- https://master.php.net/note/delete/114491/useless
Del: bad code -- https://master.php.net/note/delete/114491/bad+code
Del: spam -- https://master.php.net/note/delete/114491/spam
Del: non-english -- https://master.php.net/note/delete/114491/non-english
Del: in docs -- https://master.php.net/note/delete/114491/in+docs
Del: other reasons-- https://master.php.net/note/delete/114491
Reject -- https://master.php.net/note/reject/114491
Search -- https://master.php.net/manage/user-notes.php