Why php iteration by reference returns a duplicate last record?

I’ll guess that you’re reusing &$item here and that you’re stumbling across a behavior which has been reported as bug a thousand times but is the correct behavior of references, which is why the manual advises:

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

foreach($arrayOfJsonMods as &$item)
{
   //TODO memcached votes
}
unset($item);

See https://bugs.php.net/bug.php?id=29992

Leave a Comment