I've had all sorts of bother combining foreach with references
$testarray = array(1 => "one", 2 => "two");
$item = "three";
$testarray[3] =& $item;
foreach ($testarray as $key => $item) {
// do nothing
}
echo $testarray[3]; // outputs "two"
This really threw me off during the PHP4 era, and although it's gotten better in PHP5 by having sane behavior if you don't use explicit references, I still manage to get caught by this one now and then.