title: PHP: how to move array pointer inside foreach loop?
body: <pre><code>$animals = array('cat', 'dog', 'horse', 'elephant');
  foreach($animals as $animal)
  {
     var_dump($animal);
     next($animals);
  }
  </code></pre>
  
  <p>The code above outputs: cat, dog, horse, elephant. I thought the <code>next</code> function should move the internal pointer of <code>$animals</code> and, thus, I should be getting this output instead: cat, horse.</p>
  
  <p>How do I make the internal pointer of $animals move forward (and backwards) such that it is affected in the foreach?</p>
  
  <p>EDIT 1:</p>
  
  <p>From the manual:</p>
  
  <blockquote>
    <p>As foreach relies on the internal array pointer changing it within the
    loop may lead to unexpected behavior.</p>
  </blockquote>
  
  <p>Nonetheless, I think this is what I need to do.</p>
  
  <p>EDIT 2:</p>
  
  <p>Per "Your Common Sense"'s link, I will provide a more detailed explanation of my problem. Here's some psuedo code:</p>
  
  <pre><code>array $foos;
  
  start loop of $foos
   - do thing #1
   - do thing #2
   - do thing #3
   - keep doing thing #3 while the current value of $foos in the loop meets a certain criteria
  loop
  </code></pre>
  
  <p>When execution returns to the start of the loop, it should continue from the last array accessed by #3. <strong>Note that the array is associative</strong>, thus a <code>for ($i = 0 ...</code> approach won't work.</p>