#50161 [Bgs]: foreach needs to be fixed
| From: | marc at perkel dot com | Date: | Thu, 12 Nov 2009 21:44:54 +0000 |
| Subject: | #50161 [Bgs]: foreach needs to be fixed | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-144557@lists.php.net to get a copy of this message | ||
ID: 50161
User updated by: marc at perkel dot com
Reported By: marc at perkel dot com
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux
PHP Version: 5.2.11
New Comment:
If it's been reported several times then you aren't listening. It is a
bug. This is why open source has a bad name because people don't fix
what is obviously a bug.
Previous Comments:
------------------------------------------------------------------------
[2009-11-12 21:36:55] jani@php.net
Already reported several times, already decided to be the correct
behavior which is also documented.
------------------------------------------------------------------------
[2009-11-12 20:45:33] marc at perkel dot com
Description:
------------
When using foreach and looping through an array the second time if the
index variable isn't unset the results are that the referenced variables
is used as the index rather than the named variable.
The issue can be solved if when the foreach is set up that it does an
unset on the variable passed as the "as" variable. PHP should be changed
to unset the parameter passed as the index into the array.
Reproduce code:
---------------
$myarray = array("one","two","three","four");
foreach ($myarray as &$x) {
$x = "$x -";
print "$x\n";
}
print "\n";
foreach ($myarray as $x) {
print "$x\n";
}
Expected result:
----------------
one -
two -
three -
four -
one -
two -
three -
four -
Actual result:
--------------
one -
two -
three -
four -
one -
two -
three -
three -
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50161&edit=1