Sophie

Sophie

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

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>Count the elements 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.iterator-apply.html">iterator_apply</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.iterator-to-array.html">iterator_to_array</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-count" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">iterator_count</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">iterator_count</span> &mdash; <span class="dc-title">Count the elements in an iterator</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.iterator-count-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>iterator_count</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>
   )</div>

  <p class="para rdfs-comment">
   Count the elements in an iterator.
   <span class="function"><strong>iterator_count()</strong></span> is not guaranteed to retain the current
   position of the <code class="parameter">iterator</code>.
  </p>
 </div>


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

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

     <dd>

      <p class="para">
       The iterator being counted.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.iterator-count-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The number of elements in <code class="parameter">iterator</code>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.iterator-count-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="regexiterator.accept.example.basic">
    <p><strong>Example #1 <span class="function"><strong>iterator_count()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$iterator&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">'recipe'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">'pancakes'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'egg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'milk'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'flour'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</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>
int(4)
</pre></div>
    </div>
   </div>
   <div class="example" id="example-4843">
    <p><strong>Example #2 <span class="function"><strong>iterator_count()</strong></span> modifies position</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$iterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">([</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">var_dump</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">());<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">iterator_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</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">());<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>
string(3) &quot;one&quot;
int(3)
NULL
</pre></div>
    </div>
   </div>
   <div class="example" id="regexiterator.getflags.example.basic">
    <p><strong>Example #3 <span class="function"><strong>iterator_count()</strong></span> in <a href="control-structures.foreach.html" class="link">foreach</a> loops</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$iterator&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ArrayIterator</span><span style="color: #007700">([</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">]);<br />foreach&nbsp;(</span><span style="color: #0000BB">$iterator&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$key</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #DD0000">&nbsp;("</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">iterator_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$iterator</span><span style="color: #007700">),&nbsp;</span><span style="color: #DD0000">")\n"</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>
0: one (3)
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.iterator-apply.html">iterator_apply</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.iterator-to-array.html">iterator_to_array</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>