Unexpected behaviour in foreach while looping and unsetting in ArrayObject. An item is ignored Unexpected behaviour in foreach while looping and unsetting in ArrayObject. An item is ignored arrays arrays

Unexpected behaviour in foreach while looping and unsetting in ArrayObject. An item is ignored


From my understanding , it is considered a bad practice to modify an array while looping through it, and the proper way to do it would be using array_filter.

Since you have an ArrayObject, one solution would be to export it to an array, filter it using array_filter and create a new ArrayObject from the filtered array.

See also here : Filter ArrayObject (PHP)

Probably this behavior is due to the fact that loops are handled differently in php7. As mentioned here: http://php.net/manual/en/control-structures.foreach.php in php5 foreach uses an internal array pointer in contrast to php7.