★ wanayoo — archive 1999 http://fr.php.net/manual/de/function.array-reduce.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousarray_randarray_reversenext
Last updated: Mon, 02 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

array_reduce

(PHP 4 >= 4.0.5)

array_reduce --  Iterative Reduktion eines Arrays zu einem Wert mittels einer Callback Funktion

Beschreibung

mixed 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
add a note about notes
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 &gt; 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 :)

add a note about notes
previousarray_randarray_reversenext
Last updated: Mon, 02 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Tue Sep 10 04:20:14 2002 CEST