Bug #70635 [Nab]: an assignment operation modifies the source array
| From: | roger21 at free dot fr | Date: | Tue, 06 Oct 2015 03:21:09 +0000 |
| Subject: | Bug #70635 [Nab]: an assignment operation modifies the source array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196417@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70635&edit=1
ID: 70635
User updated by: roger21 at free dot fr
Reported by: roger21 at free dot fr
Summary: an assignment operation modifies the source array
Status: Not a bug
Type: Bug
Package: Arrays related
Operating System: linux debian 7.9 (old stable)
PHP Version: 5.4.45
Block user comment: N
Private report: N
New Comment:
holy cow, that makes sense and that sucks (i mean, i suck at php)
thanks a lot
Previous Comments:
------------------------------------------------------------------------
[2015-10-05 18:42:54] requinix@php.net
After the first foreach ends, $color will still be a reference to the last element in the $colors
array. When the second foreach starts it will reuse $color, thus updating that last element each
step through the loop.
Don't use a reference, or unset($color) after the first loop.
------------------------------------------------------------------------
[2015-10-04 11:16:53] roger21 at free dot fr
Description:
------------
libapache2-mod-php5 5.4.45-0+deb7u1
php.ini not modified
(same bug with the cli)
the problem : the assignment operation in the foreach loop from one array to another modifies the
source array (it shouldn't) - it does not if the source array is passed by reference in the
foreach loop (it shouldn't either in this case but it could have)
the test script is long because the source array is long (147) but the code is simple and obvious
i don't know if the source array matters but i guess it does, otherwise the bug would have
already been found
Test script:
---------------
http://roger21.free.fr/test/test_colors.txt
Expected result:
----------------
145 yellow
146 yellowgreen
145 yellow
146 yellowgreen
145 yellow
146 yellowgreen
Actual result:
--------------
145 yellow
146 yellowgreen
145 yellow
146 yellowgreen
145 yellow
146 yellow
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70635&edit=1