On a foreach loop, it seems PHP reads the whole array at the beginning, so if you suddenly need to append new items to the array they won’t get processed by the loop:
$a = array (1,2,3,4,5,6,7,8,9,10);
foreach ($a as $b)
{
echo " $b ";
if ($b ==5) $a[] = 11;
}
only prints out: 1 2 3 4 5 6 7 8 9 10
Just create a reference copy of the array you are looping
Or Just use
ArrayIteratorOutput
See Live Demo