downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/function.iterator-apply.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/ref.spl.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'function.iterator-apply.php',
   
1 => 'iterator_apply',
  ),
 
'up' =>
  array (
   
0 => 'ref.spl.php',
   
1 => 'SPL Functions',
  ),
 
'prev' =>
  array (
   
0 => 'function.class-parents.php',
   
1 => 'class_parents',
  ),
 
'next' =>
  array (
   
0 => 'function.iterator-count.php',
   
1 => 'iterator_count',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<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)</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>

 <a name="function.iterator-apply.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><b>iterator_apply</b></span>
    ( <span class="methodparam"><span class="type"><a href="class.traversable.php" class="type Traversable">Traversable</a></span> <tt class="parameter">$iterator</tt></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$function</tt></span>
   [, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$args</tt></span>
  ] )</div>

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


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

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">iterator</tt></i>
</span>

     </dt><dd class="listitem">

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

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">function</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       The callback function to call on every element.
       </p><blockquote><p><b class="note">Note</b>:
        <span class="simpara">
         The function must return <b><tt class="constant">TRUE</tt></b> in order to
         continue iterating over the <i><tt class="parameter">iterator</tt></i>
.
        </span>
       </p></blockquote><p>
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">args</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Arguments to pass to the callback function.
      </p>
     </dd>

   
   </dl>
<p>
  </p>

 </div>


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


 <a name="function.iterator-apply.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Example #1 <b>iterator_apply()</b> example</b></p>
    <div class="example-contents programlisting">
<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 para"><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>
  </p>
 </div>




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


</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites