array_reduce

(PHP 4 >= 4.0.5, PHP 5)

array_reduceReduce iterativamente un array a un solo valor usando una función llamada de retorno

Descripción

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

array_reduce() aplica iterativamente la función callback a los elementos de array, con el propósito de reducir el array a un solo valor.

Parámetros

array

El array de entrada.

callback
mixed callback ( mixed $carry , mixed $item )
carry

Conserva el valor de retorno de la iteración anterior; en el caso de que sea la primera iteración, conservará el valor de initial.

item

Conserva el valor de la iteración actual.

initial

Si el parámetro opcional initial está disponible, será usado al comienzo del proceso, o como un resultado final en caso de que el array esté vacío.

Valores devueltos

Devuelve el valor resultante.

Si el array está vacío y no se proporciona el parámetro initial, array_reduce() devuelve NULL.

Historial de cambios

Versión Descripción
5.3.0 Se cambió el parámetro initial para permitir mixed, anteriormente era integer.

Ejemplos

Ejemplo #1 Ejemplo de array_reduce()

<?php
function suma($carry$item)
{
    
$carry += $item;
    return 
$carry;
}

function 
multiplicación($carry$item)
{
    
$carry *= $item;
    return 
$carry;
}

$a = array(12345);
$x = array();

var_dump(array_reduce($a"suma")); // int(15)
var_dump(array_reduce($a"multiplicación"10)); // int(1200), ya que: 10*1*2*3*4*5
var_dump(array_reduce($x"suma""No hay datos a reducir")); // string(22) "No hay datos a reducir"
?>

Ver también

add a note add a note

User Contributed Notes 11 notes

up
20
Hayley Watson
7 years ago
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;
}
?>
up
7
php at keith tyler dot com
4 years ago
If you do not provide $initial, the first value used in the iteration is NULL. 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.
up
3
Seanj.jcink.com
8 years ago
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.
up
2
Altreus
9 months ago
You can effectively ignore the fact $result is passed into the callback by reference. Only the return value of the callback is accounted for.

<?php

$arr
= [1,2,3,4];

var_dump(array_reduce(
   
$arr,
    function(&
$res, $a) { $res += $a; },
   
0
));

# NULL

?>

<?php

$arr
= [1,2,3,4];

var_dump(array_reduce(
   
$arr,
    function(
$res, $a) { return $res + $a;  },
   
0
));

# int(10)
?>

Be warned, though, that you *can* accidentally change $res if it's not a simple scalar value, so despite the examples I'd recommend not writing to it at all.
up
2
ruslan dot zavackiy at gmail dot com
2 years ago
If you want something elegant in your code, when dealing with reducing array, just unshift first element, and use it as initial, because if you do not do so, you will + first element with first element:

<?php
$arr
= array(
    array(
'min' => 1.5456, 'max' => 2.28548, 'volume' => 23.152),
    array(
'min' => 1.5457, 'max' => 2.28549, 'volume' => 23.152),
    array(
'min' => 1.5458, 'max' => 2.28550, 'volume' => 23.152),
    array(
'min' => 1.5459, 'max' => 2.28551, 'volume' => 23.152),
    array(
'min' => 1.5460, 'max' => 2.28552, 'volume' => 23.152),
);

$initial = array_shift($arr);

$t = array_reduce($arr, function($result, $item) {
   
$result['min'] = min($result['min'], $item['min']);
   
$result['max'] = max($result['max'], $item['max']);
   
$result['volume'] += $item['volume'];

    return
$result;
},
$initial);
?>
up
2
yuki [dot] kodama [at] gmail [dot] com
7 years ago
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);
}
?>
up
1
bdechka at yahoo dot ca
7 years ago
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";
?>
up
1
magnesium dot oxide dot play+php at gmail dot com
8 months ago
You can reduce a two-dimensional array into one-dimensional using array_reduce and array_merge. (PHP>=5.3.0)

<?php

$two_dimensional
= array();
$two_dimensional['foo'] = array(1, 2, 3);
$two_dimensional['bar'] = array(4, 5, 6);

$one_dimensional = array_reduce($two_dimensional, 'array_merge', array());
# becomes array(1, 2, 3, 4, 5, 6)
up
0
aiadfaris at yahoo dot de
6 months ago
notice to function array_reduce()
I suppose the function rsum in the example 1 so it is not correct,
but
$ v + = $ w;
will output 15
up
0
kon
2 years ago
Walking down related object's properties using array_reduce:

<?php
  $a
=new stdClass;
 
$a->b=new stdClass;
 
$a->b->c="Hello World!\n";

 
$reductionPath=array("b","c");

 
print_r(
   
array_reduce(
     
$reductionPath,
      function(
$result, $item){
        return
$result->$item;
      },
     
$a
   
)
  );
?>
up
-2
aiadfaris at yahoo dot de
6 months ago
notice to function array_reduce()
I suppose the function rsum in the example 1 so it is not correct,
but
$ v + = $ w;
will output 15
To Top