<?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision: 1.2 $ --> <!-- splitted from ./en/functions/array.xml, last change in rev 1.2 --> <refentry id="function.array-walk"> <refnamediv> <refname>array_walk</refname> <refpurpose> Apply a user function to every member of an array </refpurpose> </refnamediv> <refsect1> <title>Description</title> <methodsynopsis> <type>int</type><methodname>array_walk</methodname> <methodparam><type>array</type><parameter>array</parameter></methodparam> <methodparam><type>string</type><parameter>func</parameter></methodparam> <methodparam choice="opt"><type>mixed</type><parameter>userdata</parameter></methodparam> </methodsynopsis> <simpara> Applies the user-defined function named by <parameter>func</parameter> to each element of <parameter>array</parameter>. <parameter>func</parameter> will be passed array value as the first parameter and array key as the second parameter. If <parameter>userdata</parameter> is supplied, it will be passed as the third parameter to the user function. <parameter>func</parameter> must be a user-defined function, and can't be a native PHP function. Thus, you can't use <function>array_walk</function> straight with <function>str2lower</function>, you must build a user-defined function with it first, and pass this function as argument. </simpara> ¬e.func-callback; <simpara> If <parameter>func</parameter> requires more than two or three arguments, depending on <parameter>userdata</parameter>, a warning will be generated each time <function>array_walk</function> calls <parameter>func</parameter>. These warnings may be suppressed by prepending the '@' sign to the <function>array_walk</function> call, or by using <function>error_reporting</function>. </simpara> <note> <para> If <parameter>func</parameter> needs to be working with the actual values of the array, specify that the first parameter of <parameter>func</parameter> should be passed by reference. Then any changes made to those elements will be made in the array itself. </para> <para> Modifying the array from inside <parameter>func</parameter> may cause unpredictable behavior. </para> </note> <note> <para> Passing the key and userdata to <parameter>func</parameter> was added in 4.0. </para> <para> In PHP 4 <function>reset</function> needs to be called as necessary since <function>array_walk</function> does not reset the array by default. </para> <para> Users may not change the array itself from the callback function. e.g. Add/delete element, unset the array that <function>array_walk</function> is applied to. If the array is changed, the behavior of this function is undefined. </para> </note> <para> <example> <title><function>array_walk</function> example</title> <programlisting role="php"> <![CDATA[ $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'); reset ($fruits); array_walk ($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; reset ($fruits); array_walk ($fruits, 'test_print'); ]]> </programlisting> <para> The printout of the program above will be: <screen role="php"> <![CDATA[ Before ...: d. lemon a. orange b. banana c. apple ... and after: d. fruit: lemon a. fruit: orange b. fruit: banana c. fruit: apple ]]> </screen> </para> </example> </para> <simpara> See also <function>each</function> and <function>list</function>. </simpara> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->