Bug #76349 [Com]: Spoils the original array
| From: | spam2 at rhsoft dot net | Date: | Thu, 17 May 2018 14:01:36 +0000 |
| Subject: | Bug #76349 [Com]: Spoils the original array | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215294@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76349&edit=1
ID: 76349
Comment by: spam2 at rhsoft dot net
Reported by: arnowt at gmail dot com
Summary: Spoils the original array
Status: Closed
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: Irrelevant
Assigned To: peehaa
Block user comment: N
Private report: N
New Comment:
this is NOT a bug - your code is buggy - it's that simple
<?php
$array1 = [1, 2, 3];
$array2 = [5, 6, 7];
foreach($array1 as $key=>&$item)
{
}
foreach($array2 as $key=>$item)
{
$item = "TEST $key";
}
print_r($array1);
print_r($array2);
$item = 'JESUS';
print_r($array1);
?>
_______________________
<?php
$array1 = [1, 2, 3];
$array2 = [5, 6, 7];
foreach($array1 as $key=>&$item)
{
}
unset($item); /** THIS IS THE FIX */
foreach($array2 as $key=>$item)
{
$item = "TEST $key";
}
print_r($array1);
print_r($array2);
$item = 'JESUS';
print_r($array1);
?>
Previous Comments:
------------------------------------------------------------------------
[2018-05-16 19:50:04] peehaa@php.net
Duplicate of https://bugs.php.net/bug.php?id=76336 and many
others
------------------------------------------------------------------------
[2018-05-16 19:27:53] arnowt at gmail dot com
Я Ð·Ð½Ð°Ñ ÑÑо Ñакое ÑÑÑлки и
иÑполÑзÑÑ Ð¸Ñ
по назнаÑениÑ.
ÐÑоÑÑо ÑпÑоÑÑил пÑимеÑ.
Ркоде Ð½ÐµÑ Ð½Ðµ единого
пÑиÑваиваниÑ, однако
иÑÑ
однÑй маÑÑив изменÑеÑÑÑ.
ÐонÑÑно ÑÑо баг можно
обойÑи, напÑÐ¸Ð¼ÐµÑ ÐµÑли оба
Ñаза иÑполÑзоваÑÑ ÑÑÑлкÑ(&$v)
пÑимеÑ:
$res=[1,2,3];
foreach($res as &$v){}
print_r($res);
foreach($res as &$v){}
print_r($res);
-------------------------------
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3 //ÑепеÑÑ Ð²Ñе ожидаемо
)
------------------------------------------------------------------------
[2018-05-16 19:14:37] spam2 at rhsoft dot net
to make it clear: you miss a unset between both loops, $v of the second one still points to the last
item of the previous loop
------------------------------------------------------------------------
[2018-05-16 19:11:39] spam2 at rhsoft dot net
just don't use references until you understand their implications and you are really sure you
gain something - given the php engine has a copy-on-write paradigm there are very few cases
------------------------------------------------------------------------
[2018-05-16 18:24:27] arnowt at gmail dot com
Description:
------------
ÐоÑÑÐ¸Ñ Ð¸ÑÑ
однÑй маÑÑив.
ÐÑовеÑÑл на веÑÑии v5.6 и v7.2
Test script:
---------------
$res=[1,2,3];
foreach($res as &$v){}
print_r($res);
foreach($res as $v){}
print_r($res);
Expected result:
----------------
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Actual result:
--------------
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 1
[1] => 2
[2] => 2 //!!!!!!
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76349&edit=1