note 118017 added to function.array-reduce
| From: | cwu at nolo dot com | Date: | Fri, 18 Sep 2015 19:34:28 +0000 |
| Subject: | note 118017 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203812@lists.php.net to get a copy of this message | ||
The single value returned by array_reduce() can be an array -- as illustrated in the following
example:
<?php
# calculate the average of an array
function calculate_sum_and_count($sum_and_count, $item)
{
list($sum, $count) = $sum_and_count;
$sum += $item;
$count += 1;
return [$sum, $count];
}
$a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$initial_sum_and_count = [0, 0];
list($sum, $count) = array_reduce($a, "calculate_sum_and_count", $initial_sum_and_count);
echo $sum / $count;
?>
----
Server IP: 72.52.91.14
Probable Submitter: 12.218.215.241
----
Manual Page -- http://php.net/manual/en/function.array-reduce.php
Edit -- https://master.php.net/note/edit/118017
Del: integrated -- https://master.php.net/note/delete/118017/integrated
Del: useless -- https://master.php.net/note/delete/118017/useless
Del: bad code -- https://master.php.net/note/delete/118017/bad+code
Del: spam -- https://master.php.net/note/delete/118017/spam
Del: non-english -- https://master.php.net/note/delete/118017/non-english
Del: in docs -- https://master.php.net/note/delete/118017/in+docs
Del: other reasons-- https://master.php.net/note/delete/118017
Reject -- https://master.php.net/note/reject/118017
Search -- https://master.php.net/manage/user-notes.php