|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-05-29 07:55 UTC] Evgeny dot Mazanik at gmail dot com
Description: ------------ --- 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. PS: A similar problem: https://bugs.php.net/bug.php?id=75173&edit=2 Test script: --------------- http://sandbox.onlinephpfunctions.com/code/ff65bf3858b153780b588476ebaa77b0aba7b4db Actual also for PHP 7.2.4, 7.2.6 Expected result: ---------------- Probably: Via array: item1 item2 item3 Via ArrayIterator: item1 item2 item3 Actual result: -------------- Via array: item1 item2 item3 Via ArrayIterator: item1 item3 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 11:00:01 2025 UTC |
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) { echo $b; $foo[] = 'f'; // Append to $foo } echo "\n",count($foo),"\t",join($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.