go to bug id or search bugs for
From manual page: http://www.php.net/arrayiterator.offsetunset
Different behavior when using Array and ArrayIterator in a foreach loop when using unset () for an associative array.
1. When using an Array in a loop and removing an element in the body of the loop, the loop is executed across all elements of the array.
2. When using ArrayIterator in a loop and deleting an element in the body of the loop, the loop is NOT performed on all elements of the array.
A similar problem:
Actual also for PHP 7.2.4, 7.2.6
Add a Patch
Add a Pull Request
For what it's worth, of course, I'll point out that this is the same difference in behaviour as when arrays vs. ArrayObjects are passed as arguments to functions. The array and the ArrayObject are both passed by value; in the array's case is the sequence of key/value pairs it is made up of; the ArrayObject's value is the ArrayObject itself.
In the same wise, the foreach() iterates over the value it is given to iterate over. That value isn't replaced while the loop is iterating. Changing the contents of an array changes its value (but the foreach doesn't see that - it's iterating over the value it was given). But changing the contents of an ArrayObject doesn't change its value - it's still the same ArrayObject, and its value is still the one given to the foreach).
$foo = ['a','b']; // The value of $foo is the sequence 0=>'a', 1=>'b'
// $foo = new ArrayIterator(['a']); // The value of $foo is an ArrayIterator object.
foreach($foo as $b)
$foo = 'f'; // Append to $foo
Bug #75173 addressed an instance of the opposite problem to the current happening: the value of the Iterator object is the Iterator object, so the loop should have been iterating over that Iterator's elements.
Duplicate of bug #70246