downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_replace_recursive> <array_rand
[edit] Last updated: Fri, 10 Feb 2012

view this page in

array_reduce

(PHP 4 >= 4.0.5, PHP 5)

array_reduceRéduit itérativement un tableau

Description

mixed array_reduce ( array $input , callback $function [, mixed $initial = NULL ] )

array_reduce() applique itérativement la fonction function aux éléments du tableau input, de manière à réduire le tableau à une valeur simple.

Liste de paramètres

input

Le tableau d'entrée.

function

La fonction de rappel.

void callback ( array &$result , mixed $item )
initial

Si l'argument optionnel initial est disponible, il sera utilisé pour initialiser le processus, ou bien comme valeur finale si le tableau est vide.

Valeurs de retour

Retourne la valeur résultante.

Si le tableau est vide et le paramètre initial n'est pas passé, array_reduce() retourne NULL.

Historique

Version Description
5.3.0 Modification du paramètre initial afin d'autoriser des variables de types mixtes ; auparavant, uniquement des entiers.

Exemples

Exemple #1 Exemple avec array_reduce()

<?php
function rsum($v$w)
{
    
$v += $w;
    return 
$v;
}

function 
rmul($v$w)
{
    
$v *= $w;
    return 
$v;
}

$a = array(12345);
$x = array();
$b array_reduce($a"rsum");
$c array_reduce($a"rmul"10);
$d array_reduce($x"rsum""Aucune donnée à réduire");
?>

Dans cet exemple, $b contiendra 15, $c contiendra 1200 (= 10*1*2*3*4*5), et $d contiendra "Aucune donnée à réduire".

Voir aussi



array_replace_recursive> <array_rand
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes array_reduce
hans+php at pleasedontspamme dot hansdude dot com 02-Nov-2011 08:18
This function is named fold in functional programming languages such as lisp, ocaml, haskell, and erlang. Python just calls it reduce.
aercolino at yahoo dot com 21-Apr-2010 07:17
Thanks to php at keith tyler dot com, knowing that "if you do not provide $initial, the first value used in the iteration is NULL", we can write callbacks that test for is_null() and take any necessary step.

Example:

<?php
function andFunc($a, $b) {
  return (
is_null($a) ? true : $a) && $b;
}
$foo = array(true, true, true);
var_dump(array_reduce($foo, "andFunc"));
?>

It returns bool(true), as expected.
php at keith tyler dot com 18-Apr-2010 03:30
It seems that $initial is a required value; if you do not provide $initial, the first value used in the iteration is NULL.

Perhaps this is not a problem for callback functions that treat NULL as an identity (e.g. addition), but is a problem for cases when NULL is not identity (such as boolean context).

Compare:

<?php
function andFunc($a,$b) {
  return
$a&&$b;
}
$foo=Array(true,true,true);
var_dump(array_reduce($foo,"andFunc"));
?>

returns false! One would expect that it would return true because true&&true&&true == true!

Adding diagnostic output to andFunc() shows that the first call to andFunc is with the arguments (NULL,true). This resolves to false (as (bool)null == false) and thereby corrupts the whole reduction.

So in this case I have to set $initial=true so that the first call to andFunc() will be (true,true). Now, if I were doing, say, orFunc(), I would have to set $initial=false. Beware.

Note that the "rmul" case in the example sneakily hides this defect! They use an $initial of 10 to get 10*1*2*3*4*5=12000. So you would assume that without an initial, you would get 1200/10 = 120 = 1*2*3*4*5. Nope! You get big fat zero, because int(null)==0, and 0*1*2*3*4*5 = 0!

I don't honestly see why array_reduce starts with a null argument. The first call to the callback should be with arguments ($initial[0],$initial[1]) [or whatever the first two array entries are], not (null,$initial[0]). That's what one would expect from the description.

Incidentally this also means that under the current implementation you will incur count($input) number of calls to the callback, not count($input)-1 as you might expect.
Hayley Watson 02-Apr-2010 06:14
The note by Hrobky immediately below applies only to PHP versions prior to 5.3.0; as noted in the function's version history, $initial is now a "mixed" parameter.

What a difference a year makes :)
Hrobky 01-Apr-2009 02:34
The initial value have to be int. So it is impossible to implement non-recursive flatten like this:

<?php
  $arr
= array(array(1), array(2), array(3));
 
$result = array_reduce($arr, 'array_merge', array());
 
// expected: $result == array (1, 2, 3)
?>

I get: Warning: array_merge() : Argument #1 is not an array.

If $initial parameter is not an int, array_reduce passes 0 (zero) to the callback function. Hayley Watson's note suggests a solution:

<?php
  $acc
= array();
  foreach(
$arr as $a)
   
$acc = array_merge($acc, $a);
 
// now: $acc == array (1, 2, 3)
?>
Hayley Watson 23-Oct-2007 02:49
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;
}
?>
Janez R. 30-Jul-2007 08:27
--REPOST, fixed some typos, please replace previous note--
In PHP ver. 4.3.1 the initial value parameter allowed for string type also. In PHP ver. 5.1.6 this param is now converted to int and callback function will receive number 0 when initial param is an empty string.

<?php
function arc ($reduced, $item)
{
 
$reduced = $item.$reduced;
  return
$reduced;
}

array_reduce( array(a,b,c), "arc", "" );
?>

Output in PHP 4.3.1: cba
Output in PHP 5.1.6: cba0

Possible solution:
<?php
function arc ($reduced, $item)
{
  if (
$reduced === 0$reduced = "";
 
$reduced = $item.$reduced;
  return
$reduced;
}
?>
yuki [dot] kodama [at] gmail [dot] com 31-Jan-2007 08:56
This code will reduce array deeply.

<?php
function print_s($s) {
    return
is_null($s) ? "NULL" : (is_array($s) ? "Array" : ($s ? "TRUE" : "FALSE"));
}
function
r_and_dp($a, $b) {
    echo
"phase1:" . print_s($a) . "," . print_s($b) . "<br>\n";
    if(
is_array($a)) {
       
$a = array_reduce($a, "r_and_dp");
    }
    if(
is_array($b)) {
       
$b = array_reduce($b, "r_and_dp");
    }
    echo
"phase2:" . print_s($a) . "," . print_s($b) . "<br>\n";
   
$a = is_null($a) ? TRUE : $a;
   
$b = is_null($b) ? TRUE : $b;
    echo
"phase3:" . print_s($a) . "," . print_s($b) . "<br>\n";
    return
$a && $b;
}
$bools = array(TRUE, array(FALSE, TRUE), TRUE);
echo
print_s(array_reduce($bools, "r_and_dp")) . "<br>\n";

// result: FALSE
?>

When using boolean, you have to carefully set an "initial" argument.

<?php
function r_or_dp($a, $b) {
    if(
is_array($a)) {
       
$a = array_reduce($a, "r_or_dp");
    }
    if(
is_array($b)) {
       
$b = array_reduce($b, "r_or_dp");
    }
    return (
is_null($a) ? FALSE : $a) || (is_null($b) ? FALSE : $b);
}
?>
bdechka at yahoo dot ca 10-Jan-2007 12:57
The above code works better this way.

<?php
function reduceToTable($html, $p) {
  
$html .= "<TR><TD><a href=\"$p.html\">$p</a></td></tr>\n";
   return
$html;
}

$list = Array("page1", "page2", "page3");

$tab = array_reduce($list, "reduceToTable");
echo
"<table>".$tab . "</table>\n";
?>
marcel dot oehler at kubusmedia dot com 08-Feb-2006 07:26
I've just experienced some really strange behaviour of array_reduce in PHP 5.0.4:

$result = array( 0, 17, 0, 0, 33, 0, 0, 0, 0, 50);
$total = array_reduce( $result, "sumCalc", 0);

function sumCalc( $a, $b){
    return $a + $b;
}

and $total equals to 83!

I know, this could be done easier, but it should work nevertheless. Has anybody experienced something similar? I will avoid using array_reduce in the future...
Seanj.jcink.com 04-Jan-2006 06:23
The code posted below by bishop to count the characters of an array is simply... erm... well useless to me...

$array=Array("abc","de","f");
strlen(implode("",$array)); //6

works; and is much smaller. Probably much faster too.
ildar [DASH] sh [AT] mail [DOT] ru 29-Nov-2005 10:02
in rare cases when an array is a set of numeric values and result is one of sum or product of numbers the next examples may be useful

<?php

// sum of array items
echo eval('return ' . implode('+', $nums) . ';');

// product of array items
echo eval('return ' . implode('*', $nums) . ';');

?>

the reason of these codes is omitting of single used per script of callbacks
david dot tulloh at infaze dot com dot au 23-Jun-2005 01:18
The code supplied by cuntbubble is unfortunately incorrect.

Running it I got the output:
0<TR><TD><a href="page1.html">page1</a></td>
<TR><TD><a href="page2.html">page2</a></td>
<TR><TD><a href="page3.html">page3</a></td>
</table>

So php, not finding an integer, used int(0) to start the process.  I've tested to confirm this.
bishop 30-Apr-2004 08:19
Count the total number of characters in an array of strings:

<?php
$lines
= array ('abc', 'd', 'ef');
$totalChars = array_reduce($lines, create_function('$v,$w','return $v + strlen($w);'), 0);
// $totalChars === 6
?>
tonicpeddler at aol dot com 01-Nov-2002 07:17
in response to php dot net at cuntbubble dot com

actually when you pass a value to a function that accepts a specific data type, php automatically evaluates that value as the data type expected
php dot net at cuntbubble dot com 17-Jan-2002 08: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

<?php
function reduceToTable($html, $p) {
   
$html .= "<TR><TD><a href=\"$p.html\">$p</a></td>\n";
    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 :)

 
show source | credits | sitemap | contact | advertising | mirror sites