Bug #73261 [NEW]: Looping through array with pointer inadvertently breaks array.
| From: | randy at larsongroup dot org | Date: | Thu, 06 Oct 2016 21:08:39 +0000 |
| Subject: | Bug #73261 [NEW]: Looping through array with pointer inadvertently breaks array. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-204520@lists.php.net to get a copy of this message | ||
From: randy at larsongroup dot org
Operating system: Tested on OSX and Ubuntu 12.04
PHP version: 5.6.26
Package: Arrays related
Bug Type: Bug
Bug description:Looping through array with pointer inadvertently breaks array.
Description:
------------
When you loop through an array with foreach and set the value as a
pointer to the array value then the last element in the array will be
set as a pointer to the second to last element of the array.
Then if you loop through the array a second time you will see that the
last value isn't there, but points to the second to last value.
Test script:
---------------
<?php
$originalArray = array('Thor', 'The Hulk', 'Iron Man');
var_dump($originalArray);
foreach ($originalArray as &$value) {
echo $value."\n";
}
echo "\nSecond time through loop\n";
foreach ($originalArray as $value) {
echo $value."\n";
}
// notice the last element is a pointer which it shouldn't be.
echo "\n".var_dump($originalArray);
Expected result:
----------------
array(3) {
[0]=>
string(4) "Thor"
[1]=>
string(8) "The Hulk"
[2]=>
string(8) "Iron Man"
}
Thor
The Hulk
Iron Man
Second time through loop
Thor
The Hulk
Iron Man
array(3) {
[0]=>
string(4) "Thor"
[1]=>
string(8) "The Hulk"
[2]=>
string(8) "Iron Man"
}
Actual result:
--------------
array(3) {
[0]=>
string(4) "Thor"
[1]=>
string(8) "The Hulk"
[2]=>
string(8) "Iron Man"
}
Thor
The Hulk
Iron Man
Second time through loop
Thor
The Hulk
The Hulk
array(3) {
[0]=>
string(4) "Thor"
[1]=>
string(8) "The Hulk"
[2]=>
&string(8) "The Hulk"
}
--
Edit bug report at https://bugs.php.net/bug.php?id=73261&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73261&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73261&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73261&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=73261&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=73261&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=73261&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=73261&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=73261&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=73261&r=support
Expected behavior: https://bugs.php.net/fix.php?id=73261&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=73261&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=73261&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=73261&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73261&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=73261&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=73261&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=73261&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73261&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=73261&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=73261&r=mysqlcfg