|
|
 |
array_reduce (PHP 4 >= 4.0.5) array_reduce --
Iterative Reduktion eines Arrays zu einem Wert mittels einer
Callback Funktion
Beschreibungmixed array_reduce ( array input, mixed callback [, int initial])
array_reduce() wendet die
callback Funktion iterativ bei den
Elementen des Arrays input so an, dass das
Array auf einen einzigen Wert reduziert wird. Ist der optionale
Parameter intial angegeben, wird er am
Beginn des Prozesses, benutzt oder als Resultat verwendet, sollte
das Array leer sein.
Beispiel 1. array_reduce() function rsum($v, $w) {
$v += $w;
return $v;
}
function rmul($v, $w) {
$v *= $w;
return $v;
}
$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1); |
|
Hier enthält $b 15,
$c 1200 (= 1*2*3*4*5*10),
und $d enthält 1.
Siehe auch array_filter() und
array_map().
User Contributed Notes array_reduce |
 |
php.net@cuntbubble.com
18-Jan-2002 03:08 |
|
There is an error/misleading item in the documentation
[, int
initial]
int is not constrained to an integer, it can be any data
type (although I've not tested ALL data types)
and $v is the
cumulative part, the current value of the reduction.
and I'll take
the liberty to add another example, as used in my code
function
reduceToTable($html, $p) { $html .= "<TR><TD><a
href=/old?u=http%3A%2F%2Ffr.php.net%2Fmanual%2Fde%2F%5C%26quot%3B%24p.html%5C%26quot%3B%26gt%3B%24p%26lt%3B%2Fa%26gt%3B%26lt%3B%2Ftd%26gt%3B%5Cn%26quot%3B%3B%3Cbr&y=1999>
return $html; }
$list = Array("page1",
"page2", "page3");
$tab = array_reduce($list,
"reduceToTable", "<table>\n"); echo $tab .
"</table>\n";
hmm, getting stuff on one line sure is
tricky, it get's wordwrapped on the char count in html so > counts
as 4 chars not one so by the time you've counted "< you've used up
8 chars If it get's through moderation could someone please make it look
ok :)
|
|
 |
| |