Sophie

Sophie

distrib > Mageia > 7 > x86_64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 4354

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Call a function for every element in an iterator</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.class-uses.html">class_uses</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.iterator-count.html">iterator_count</a></div>
 <div class="up"><a href="ref.spl.html">SPL Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.iterator-apply" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">iterator_apply</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">iterator_apply</span> &mdash; <span class="dc-title">Call a function for every element in an iterator</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.iterator-apply-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>iterator_apply</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.traversable.html" class="type Traversable">Traversable</a></span> <code class="parameter">$iterator</code></span>
   , <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$function</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$args</code><span class="initializer"> = <strong><code>NULL</code></strong></span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Calls a function for every element in an iterator.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.iterator-apply-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">iterator</code></dt>

     <dd>

      <p class="para">
       The iterator object to iterate over.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">function</code></dt>

     <dd>

      <p class="para">
       The callback function to call on every element.
       This function only receives the given <code class="parameter">args</code>, so it
       is nullary by default. If <em>count($args) === 3</em>, for
       instance, the callback function is ternary.
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <span class="simpara">
         The function must return <strong><code>TRUE</code></strong> in order to
         continue iterating over the <code class="parameter">iterator</code>.
        </span>
       </p></blockquote>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">args</code></dt>

     <dd>

      <p class="para">
       An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of arguments; each element of
       <code class="parameter">args</code> is passed to the callback
       <code class="parameter">function</code> as separate argument.
      </p>
     </dd>

    
   </dl>

  </p>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.iterator-apply-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the iteration count.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.iterator-apply-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-4841">
    <p><strong>Example #1 <span class="function"><strong>iterator_apply()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">print_caps</span><span style="color: #007700">(</span><span style="color: #0000BB">Iterator&nbsp;$iterator</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">strtoupper</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">current</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$it&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"Apples"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Bananas"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"Cherries"</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">iterator_apply</span><span style="color: #007700">(</span><span style="color: #0000BB">$it</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"print_caps"</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">$it</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="cdata"><pre>
APPLES
BANANAS
CHERRIES
</pre></div>
    </div>
   </div>
  </p>
 </div>




 <div class="refsect1 seealso" id="refsect1-function.iterator-apply-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - Apply a user supplied function to every member of an array</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.class-uses.html">class_uses</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.iterator-count.html">iterator_count</a></div>
 <div class="up"><a href="ref.spl.html">SPL Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>