mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 08:58:56 +00:00
Fixed typo; rewrote most of docs; see also foreach() and call_user_func_array().
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@100035 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
891e8eb600
commit
9dea6b1a31
1 changed files with 50 additions and 41 deletions
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- $Revision: 1.4 $ -->
|
||||
<!-- $Revision: 1.5 $ -->
|
||||
<!-- splitted from ./en/functions/array.xml, last change in rev 1.2 -->
|
||||
<refentry id="function.array-walk">
|
||||
<refnamediv>
|
||||
|
@ -17,62 +17,68 @@
|
|||
<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.
|
||||
Applies the user-defined function <parameter>func</parameter> to each
|
||||
element of the <parameter>array</parameter> array. Typically,
|
||||
<parameter>func</parameter> takes on two parameters.
|
||||
The <parameter>array</parameter> parameter's value being the first, and
|
||||
the key/index second. If the optional <parameter>userdata</parameter>
|
||||
parameter is supplied, it will be passed as the third parameter to
|
||||
function <parameter>func</parameter>.
|
||||
</simpara>
|
||||
<simpara>
|
||||
<parameter>func</parameter> must be a user-defined function, not a
|
||||
built-in PHP function such as <function>strtolower</function> or
|
||||
<function>stripslashes</function>. The user-defined function can
|
||||
use built-in PHP functions.
|
||||
</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>.
|
||||
If function <parameter>func</parameter> requires more parameters than
|
||||
given to it, an error of level <link linkend="errorfunc.constants">
|
||||
E_WARNING</link> will be generated each time <function>array_walk</function>
|
||||
calls <parameter>func</parameter>. These warnings may be suppressed by
|
||||
prepending the PHP error operator
|
||||
<link linkend="language.operators.errorcontrol">@</link> 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.
|
||||
actual values of the array, specify the first parameter of
|
||||
<parameter>func</parameter> as a
|
||||
<link linkend="language.references">reference</link>. Then,
|
||||
any changes made to those elements will be made in the
|
||||
original array itself.
|
||||
</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.
|
||||
added in 4.0.0
|
||||
</para>
|
||||
</note>
|
||||
<para>
|
||||
<function>array_walk</function> is not affected by the internal
|
||||
array pointer of <parameter>array</parameter>. <function>
|
||||
array_walk</function> will walk through the entire array
|
||||
regardless of pointer position. To reset the pointer, use
|
||||
<function>reset</function>. In PHP 3,
|
||||
<function>array_walk</function> resets the pointer.
|
||||
</para>
|
||||
<para>
|
||||
Users may not change the array itself from the callback
|
||||
function. e.g. Add/delete elements, unset elements, etc. If
|
||||
the array that <function>array_walk</function> is applied to
|
||||
is changed, the behavior of this function is undefined, and
|
||||
unpredictable.
|
||||
</para>
|
||||
<para>
|
||||
<example>
|
||||
<title><function>array_walk</function> example</title>
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
$fruits = array ("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
|
||||
|
||||
function test_alter (&$item1, $key, $prefix) {
|
||||
|
@ -82,13 +88,15 @@ function test_alter (&$item1, $key, $prefix) {
|
|||
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>
|
||||
|
@ -111,7 +119,8 @@ c. fruit: apple
|
|||
</example>
|
||||
</para>
|
||||
<simpara>
|
||||
See also <function>each</function> and <function>list</function>.
|
||||
See also <function>list</function>, <link linkend="control-structures.foreach">foreach</link>,
|
||||
<function>each</function>, and <function>call_user_func_array</function>.
|
||||
</simpara>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
|
|
Loading…
Reference in a new issue