<HTML ><HEAD ><TITLE >array_walk</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_values" HREF="function.array-values.html"><LINK REL="NEXT" TITLE="array" HREF="function.array.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" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-values.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-walk" ></A >array_walk</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8316" ></A ><P > (PHP 3>= 3.0.3, PHP 4 )</P >array_walk -- Apply a user function to every member of an array </DIV ><DIV CLASS="refsect1" ><A NAME="AEN8319" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >array_walk</B > ( array array, callback function [, mixed userdata])<BR ></BR ><P > Applies the user-defined function <TT CLASS="parameter" ><I >function</I ></TT > to each element of the <TT CLASS="parameter" ><I >array</I ></TT > array. Typically, <TT CLASS="parameter" ><I >function</I ></TT > takes on two parameters. The <TT CLASS="parameter" ><I >array</I ></TT > parameter's value being the first, and the key/index second. If the optional <TT CLASS="parameter" ><I >userdata</I ></TT > parameter is supplied, it will be passed as the third parameter to the callback <TT CLASS="parameter" ><I >function</I ></TT >. </P ><P > If function <TT CLASS="parameter" ><I >function</I ></TT > requires more parameters than given to it, an error of level <A HREF="ref.errorfunc.html#errorfunc.constants" > E_WARNING</A > will be generated each time <B CLASS="function" >array_walk()</B > calls <TT CLASS="parameter" ><I >function</I ></TT >. These warnings may be suppressed by prepending the PHP error operator <A HREF="language.operators.errorcontrol.html" >@</A > to the <B CLASS="function" >array_walk()</B > call, or by using <A HREF="function.error-reporting.html" ><B CLASS="function" >error_reporting()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > If <TT CLASS="parameter" ><I >function</I ></TT > needs to be working with the actual values of the array, specify the first parameter of <TT CLASS="parameter" ><I >function</I ></TT > as a <A HREF="language.references.html" >reference</A >. Then, any changes made to those elements will be made in the original array itself. </P ></BLOCKQUOTE ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Passing the key and userdata to <TT CLASS="parameter" ><I >function</I ></TT > was added in 4.0.0 </P ></BLOCKQUOTE ></DIV ><P > <B CLASS="function" >array_walk()</B > is not affected by the internal array pointer of <TT CLASS="parameter" ><I >array</I ></TT >. <B CLASS="function" > array_walk()</B > will walk through the entire array regardless of pointer position. To reset the pointer, use <A HREF="function.reset.html" ><B CLASS="function" >reset()</B ></A >. In PHP 3, <B CLASS="function" >array_walk()</B > resets the pointer. </P ><P > Users may not change the array itself from the callback function. e.g. Add/delete elements, unset elements, etc. If the array that <B CLASS="function" >array_walk()</B > is applied to is changed, the behavior of this function is undefined, and unpredictable. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8365" ></A ><P ><B >Example 1. <B CLASS="function" >array_walk()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple"); function test_alter (&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print ($item2, $key) { echo "$key. $item2<br>\n"; } echo "Before ...:\n"; array_walk ($fruits, 'test_print'); array_walk ($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk ($fruits, 'test_print'); ?></PRE ></TD ></TR ></TABLE ><P > The printout of the program above will be: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.list.html" ><B CLASS="function" >list()</B ></A >, <A HREF="control-structures.foreach.html" >foreach</A >, <A HREF="function.each.html" ><B CLASS="function" >each()</B ></A >, and <A HREF="function.call-user-func-array.html" ><B CLASS="function" >call_user_func_array()</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-values.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_values</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array</TD ></TR ></TABLE ></DIV ></BODY ></HTML >