<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.array-walk.php',
    1 => 'array_walk',
    2 => 'Apply a user supplied function to every member of an array',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'prev' => 
  array (
    0 => 'function.array-values.php',
    1 => 'array_values',
  ),
  'next' => 
  array (
    0 => 'function.array-walk-recursive.php',
    1 => 'array_walk_recursive',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/array-walk.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.array-walk" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_walk</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_walk</span> &mdash; <span class="dc-title">Apply a user supplied function to every member of an array</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-walk-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_walk</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.object.php" class="type object">object</a></span></span> <code class="parameter reference">&$array</code></span>, <span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$arg</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span></div>

  <p class="simpara">
   Applies the user-defined <code class="parameter">callback</code> function to each
   element of the <code class="parameter">array</code> array.
  </p>
  <p class="para">
   <span class="function"><strong>array_walk()</strong></span> is not affected by the internal array
   pointer of <code class="parameter">array</code>.  <span class="function"><strong>array_walk()</strong></span>
   will walk through the entire array regardless of pointer position.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-walk-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="para">
       The input array.
      </p>
     </dd>
    
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
       Typically, <code class="parameter">callback</code> takes on two parameters.
       The <code class="parameter">array</code> parameter&#039;s value being the first, and
       the key/index second.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        If <code class="parameter">callback</code> needs to be working with the
        actual values of the array, specify the first parameter of
        <code class="parameter">callback</code> as a
        <a href="language.references.php" class="link">reference</a>. Then,
        any changes made to those elements will be made in the
        original array itself.
       </p>
      </p></blockquote>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
        <p class="para">
          Many internal functions (for example <span class="function"><a href="function.strtolower.php" class="function">strtolower()</a></span>)
          will throw if more than the expected number of arguments
          are passed in and are not usable directly as a
          <code class="parameter">callback</code>.
        </p>
      </p></blockquote>
      <p class="para">
       Only the values of the <code class="parameter">array</code> may potentially be
       changed; its structure cannot be altered, i.e., the programmer cannot
       add, unset or reorder elements. If the callback does not respect this
       requirement, the behavior of this function is undefined, and      
       unpredictable.
      </p>
     </dd>
    
    
     <dt><code class="parameter">arg</code></dt>
     <dd>
      <p class="para">
       If the optional <code class="parameter">arg</code> parameter is supplied,
       it will be passed as the third parameter to the
       <code class="parameter">callback</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-walk-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Always returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.array-walk-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   As of PHP 7.1.0, an <span class="classname"><a href="class.argumentcounterror.php" class="classname">ArgumentCountError</a></span> will be thrown if the <code class="parameter">callback</code> function 
   requires more than 2 parameters (the value and key of the array member),
   or more than 3 parameters if the <code class="parameter">arg</code> is also passed.
   Previously, in this case
   an error of level <a href="errorfunc.constants.php" class="link">E_WARNING</a> would be generated each time 
   <span class="function"><strong>array_walk()</strong></span> calls <code class="parameter">callback</code>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-walk-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.2.0</td>
 <td>
  The return type is <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> now; previously, it was <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span>.
 </td>
</tr>

     <tr>
      <td>8.0.0</td>
      <td>
       If <code class="parameter">callback</code> expects the second or third parameter to be passed
       by reference, this function will now emit an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.array-walk-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>array_walk()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fruits </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"d" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"lemon"</span><span style="color: #007700">, </span><span style="color: #DD0000">"a" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">, </span><span style="color: #DD0000">"c" </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">test_alter</span><span style="color: #007700">(&amp;</span><span style="color: #0000BB">$item1</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$prefix</span><span style="color: #007700">)<br />{<br />    </span><span style="color: #0000BB">$item1 </span><span style="color: #007700">= </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$prefix</span><span style="color: #DD0000">: </span><span style="color: #0000BB">$item1</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">test_print</span><span style="color: #007700">(</span><span style="color: #0000BB">$item2</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">)<br />{<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">. </span><span style="color: #0000BB">$item2</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />}<br /><br />echo </span><span style="color: #DD0000">"Before ...:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">array_walk</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 /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$fruits</span><span style="color: #007700">, </span><span style="color: #DD0000">'test_alter'</span><span style="color: #007700">, </span><span style="color: #DD0000">'fruit'</span><span style="color: #007700">);<br />echo </span><span style="color: #DD0000">"... and after:\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">array_walk</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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Example #2 <span class="function"><strong>array_walk()</strong></span> example using anonymous function</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$elements </span><span style="color: #007700">= [</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">array_walk</span><span style="color: #007700">(</span><span style="color: #0000BB">$elements</span><span style="color: #007700">, function (</span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">) {<br />  echo </span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$key</span><span style="color: #007700">}</span><span style="color: #DD0000"> =&gt; </span><span style="color: #007700">{</span><span style="color: #0000BB">$value</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">0 =&gt; a
1 =&gt; b
2 =&gt; c</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-walk-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.array-walk-recursive.php" class="function" rel="rdfs-seeAlso">array_walk_recursive()</a> - Apply a user function recursively to every member of an array</span></li>
    <li><span class="function"><a href="function.iterator-apply.php" class="function" rel="rdfs-seeAlso">iterator_apply()</a> - Call a function for every element in an iterator</span></li>
    <li><span class="function"><a href="function.list.php" class="function" rel="rdfs-seeAlso">list()</a> - Assign variables as if they were an array</span></li>
    <li><span class="function"><a href="function.each.php" class="function" rel="rdfs-seeAlso">each()</a> - Return the current key and value pair from an array and advance the array cursor</span></li>
    <li><span class="function"><a href="function.call-user-func-array.php" class="function" rel="rdfs-seeAlso">call_user_func_array()</a> - Call a callback with an array of parameters</span></li>
    <li><span class="function"><a href="function.array-map.php" class="function" rel="rdfs-seeAlso">array_map()</a> - Applies the callback to the elements of the given arrays</span></li>
    <li><a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>