title: Dynamic add to current foreach array
body: <p>I have a loop that needs to go through each item. So naturally a foreach loop seems like the best idea. However, I need to add an element to the array as it iterates. I tried the following without any luck.</p>
  
  <pre><code>foreach ($allitems as $item) {
      //Do some stuff here
  
      if ($value === true)
          $allitems[] = 'New item';
  }
  </code></pre>
  
  <p>I found out the foreach loops seem to use a referenced copy of the array, so editing the array does not register in the loop.</p>
  
  <p>A workaround is to use the older styled while loops as follows:</p>
  
  <pre><code>while (list($key, $item) = each($allitems)) {
      //Do some stuff here
  
      if ($value === true)
          $allitems[] = 'New item';
  }
  </code></pre>
  
  <p>Clearly a foreach loop would be nicer and more efficient. Is it possible? Or is the while structure the best possible solution.</p>