note 78695 added to function.array-reduce

From: Date: Tue, 23 Oct 2007 21:49:04 +0000
Subject: note 78695 added to function.array-reduce
Groups: php.notes 
Request: Send a blank email to php-notes+get-131670@lists.php.net to get a copy of this message
To make it clearer about what the two parameters of the callback are for, and what "reduce to a single value" actually means (using associative and commutative operators as examples may obscure this). 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. The second parameter is where each value of the array is passed during each step of the reduction. The return value of the callback becomes the new value of the accumulator. When the array is exhausted, array_reduce() returns accumulated value. If you carried out the reduction by hand, you'd get something like the following lines, every one of which therefore producing the same result: <?php array_reduce(array(1,2,3,4), 'f', 99 ); array_reduce(array(2,3,4), 'f', f(99,1) ); array_reduce(array(3,4), 'f', f(f(99,1),2) ); array_reduce(array(4), 'f', f(f(f(99,1),2),3) ); array_reduce(array(), 'f', f(f(f(f(99,1),2),3),4) ); f(f(f(f(99,1),2),3),4) ?> If you made function f($v,$w){return "f($v,$w)";} the last line would be the literal result. A PHP implementation might therefore look something like this (less details like error checking and so on): <?php function array_reduce($array, $callback, $initial=null) { $acc = $initial; foreach($array as $a) $acc = $callback($acc, $a); return $acc; } ?> ---- Server IP: 202.89.58.1 Probable Submitter: 60.234.168.92 ---- Manual Page -- http://www.php.net/manual/en/function.array-reduce.php Edit -- https://master.php.net/note/edit/78695 Del: integrated -- https://master.php.net/note/delete/78695/integrated Del: useless -- https://master.php.net/note/delete/78695/useless Del: bad code -- https://master.php.net/note/delete/78695/bad+code Del: spam -- https://master.php.net/note/delete/78695/spam Del: non-english -- https://master.php.net/note/delete/78695/non-english Del: in docs -- https://master.php.net/note/delete/78695/in+docs Del: other reasons-- https://master.php.net/note/delete/78695 Reject -- https://master.php.net/note/reject/78695 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131670) next »