<HTML ><HEAD ><TITLE >array_reduce</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual de PHP" HREF="index.html"><LINK REL="UP" TITLE="Funciones de matrices" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_rand" HREF="function.array-rand.html"><LINK REL="NEXT" TITLE="array_reverse" HREF="function.array-reverse.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual de PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-rand.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-reverse.html" ACCESSKEY="N" >Siguiente</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-reduce" ></A >array_reduce</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN5367" ></A ><P > (PHP 4 >= 4.0.5)</P >array_reduce -- Iteratively reduce the array to a single value using a callback function </DIV ><DIV CLASS="refsect1" ><A NAME="AEN5370" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >array_reduce</B > ( array input, callback function [, int initial])<BR ></BR ><P > <B CLASS="function" >array_reduce()</B > applies iteratively the <TT CLASS="parameter" ><I >function</I ></TT > function to the elements of the array <TT CLASS="parameter" ><I >input</I ></TT >, so as to reduce the array to a single value. If the optional <TT CLASS="parameter" ><I >initial</I ></TT > is available, it will be used at the beginning of the process, or as a final result in case the array is empty. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN5390" ></A ><P ><B >Ejemplo 1. <B CLASS="function" >array_reduce()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="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);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > This will result in <TT CLASS="varname" >$b</TT > containing <TT CLASS="literal" >15</TT >, <TT CLASS="varname" >$c</TT > containing <TT CLASS="literal" >1200</TT > (= 1*2*3*4*5*10), and <TT CLASS="varname" >$d</TT > containing <TT CLASS="literal" >1</TT >. </P ><P > See also <A HREF="function.array-filter.html" ><B CLASS="function" >array_filter()</B ></A > and <A HREF="function.array-map.html" ><B CLASS="function" >array_map()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.array-rand.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Inicio</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-reverse.html" ACCESSKEY="N" >Siguiente</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_rand</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Subir</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_reverse</TD ></TR ></TABLE ></DIV ></BODY ></HTML >