📄 function.array-walk-recursive.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Apply a user function recursively to every member of an array</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-values.html">array_values</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-walk.html">array_walk</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.array-walk-recursive" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_walk_recursive</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">array_walk_recursive</span> — <span class="dc-title">Apply a user function recursively to every member of an array</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>array_walk_recursive</b></b></span> ( <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$input</tt></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$funcname</tt></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$userdata</tt></span> ] )</div> <p class="para rdfs-comment"> Applies the user-defined function <i><tt class="parameter">funcname</tt></i> to each element of the <i><tt class="parameter">input</tt></i> array. This function will recur into deeper arrays. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">input</tt></i></span> <dd> <p class="para"> The input array. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">funcname</tt></i></span> <dd> <p class="para"> Typically, <i><tt class="parameter">funcname</tt></i> takes on two parameters. The <i><tt class="parameter">input</tt></i> parameter's value being the first, and the key/index second. </p> <blockquote><p><b class="note">Note</b>: If <i><tt class="parameter">funcname</tt></i> needs to be working with the actual values of the array, specify the first parameter of <i><tt class="parameter">funcname</tt></i> as a <a href="language.references.html" class="link">reference</a>. Then, any changes made to those elements will be made in the original array itself. <br /> </p></blockquote> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">userdata</tt></i></span> <dd> <p class="para"> If the optional <i><tt class="parameter">userdata</tt></i> parameter is supplied, it will be passed as the third parameter to the callback <i><tt class="parameter">funcname</tt></i>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>array_walk_recursive()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$sweet </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'banana'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fruits </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'sweet' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$sweet</span><span style="color: #007700">, </span><span style="color: #DD0000">'sour' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'lemon'</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">test_print</span><span style="color: #007700">(</span><span style="color: #0000BB">$item</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br /> echo </span><span style="color: #DD0000">"$key holds $item\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">array_walk_recursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">, </span><span style="color: #DD0000">'test_print'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>a holds appleb holds bananasour holds lemon</pre></div> </pre></div> <div class="example-contents"><p> You may notice that the key 'sweet' is never displayed. Any key that holds an <a href="language.types.array.html" class="type array">array</a> will not be passed to the function. </p></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a></li> <li class="member">information about the <a href="language.pseudo-types.html#language.types.callback" class="link">callback</a> type</li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-values.html">array_values</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-walk.html">array_walk</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -