array_reduce

(PHP 4 >= 4.0.5, PHP 5)

array_reduce -- 用回调函数迭代地将数组简化为单一的值

说明

mixed array_reduce ( array input, callback function [, int initial] )

array_reduce() 将回调函数 function 迭代地作用到 input 数组中的每一个单元中,从而将数组简化为单一的值。如果指定了可选参数 initial,该参数将被当成是数组中的第一个值来处理,或者如果数组为空的话就作为最终返回值。如果数组为空并且没有传递 initial 参数,array_reduce() 返回 NULL

例子 1. array_reduce() 例子

<?php
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);
?>

这将使 $b 的值为 15$c 的值为 1200(= 10*1*2*3*4*5),以及 $d 的值为 1

参见 array_filter()array_map()array_unique()array_count_values()


add a note add a note User Contributed Notes
marcel dot oehler at kubusmedia dot com
08-Feb-2006 11: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
05-Jan-2006 10: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
30-Nov-2005 02: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 04: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
01-May-2004 11: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
02-Nov-2002 11: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
18-Jan-2002 12: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 :)