Bug #54189 [Com]: foreach changes array values after iterating by reference followed by foreach

From: Date: Thu, 01 Jan 2015 20:29:20 +0000
Subject: Bug #54189 [Com]: foreach changes array values after iterating by reference followed by foreach
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189585@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54189&edit=1

 ID:                 54189
 Comment by:         chealer at gmail dot com
 Reported by:        phazei at gmail dot com
 Summary:            foreach changes array values after iterating by
                     reference followed by foreach
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   RHEL 5.6
 PHP Version:        5.2.17
 Block user comment: N
 Private report:     N

 New Comment:

The problematic behavior behind this issue is being tracked in #62132.


Previous Comments:
------------------------------------------------------------------------
[2011-03-08 09:24:34] scottmac@php.net

This is on the foreach manual page.

Between the two foreach loops, $value is a reference to the last entry in the 
array.

When the second loop starts you then start assigning new values to $value which 
reference the last element of the array.

------------------------------------------------------------------------
[2011-03-08 08:11:41] phazei at gmail dot com

Description:
------------
(I searched through the bug reports and found some foreach pointer issues, but 
none related to this)

If I simply go through an array with a referenced value like so:
foreach ($array as &$value) {  }

And then go through it again, not referenced, like so:
foreach ($array as $value) {  }

Both times using the same variable $value, then during the second foreach, the 
second to the last value is actually copied over the last value changing the 
array.

If I simply use $value2 there is no issue.

Test script:
---------------
$array =
array('aaaaa','bbbbb','ccccc','ddddd','eeeee');
echo '1) '.print_r($array, true).'<br>';
foreach ($array as &$value) {  }
echo '2) '.print_r($array, true).'<br> 3) ';
foreach ($array as $key => $value) { echo "[$key] => $value "; }
echo '<br> 4) '.print_r($array, true).'<br>';

Expected result:
----------------
1) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 
2) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 
3) [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee 
4) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 

Actual result:
--------------
1) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 
2) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => eeeee ) 
3) [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => ddddd 
4) Array ( [0] => aaaaa [1] => bbbbb [2] => ccccc [3] => ddddd [4] => ddddd ) 


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=54189&edit=1


Thread (3 messages)

« previous php.bugs (#189585) next »