Manipulating the array you're iterating over is generally not a good idea. If what you want is to process the elements of the array beginning from the end, use either while as advised by psini or use for/foreach on a reversed list (and do not pop the elements, it's not necessary).