Multiple foreach() with references in php

It took me a couple of hours to debug : I had an array with values that
shouldn't be here, values from another part of the application, and I couldn't
figure out where that came from.

Well, it turns it was part of a documented feature of PHP :

Unless the array is referenced, foreach operates on a copy of the specified
array and not the array itself. foreach has some side effects on the array
pointer. Don't rely on the array pointer during or after the foreach without
resetting it. Reference of a $value and the last array element remain even
after the foreach loop. It is recommended to destroy it by unset().php.net