Bug #72946 [Opn]: variable assignment failure with foreach after reference
| From: | laruence@php.net | Date: | Fri, 26 Aug 2016 10:06:00 +0000 |
| Subject: | Bug #72946 [Opn]: variable assignment failure with foreach after reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-203565@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72946&edit=1
ID: 72946
Updated by: laruence@php.net
Reported by: shaveneLee at gmail dot com
Summary: variable assignment failure with foreach after
reference
Status: Open
Type: Bug
Package: *General Issues
Operating System: Windows 10 or Centos 6.8
PHP Version: 7.0.10
Block user comment: N
Private report: N
New Comment:
from https://3v4l.org/CafJM , I don't see the problem, do
you have any thirdpart extension loaded which may affect the behavior?
Previous Comments:
------------------------------------------------------------------------
[2016-08-26 09:28:19] shaveneLee at gmail dot com
Description:
------------
same array variable($reqs) when it was assign value with &, then use foreach to loop it again,
the last loop variable value was assigned failed.
please,see the simple test script below.
Test script:
---------------
function foo() {
$reqs = array('11', '22', array('id' => 123,
'name' => 'ffff'));
foreach ($reqs as &$req) {
if (is_array($req)) {
$req['name'] = 'dd'; //alter value
}
}
echo '<pre>';
print_r($reqs); //values changed, ok
echo '</pre>';
foreach ($reqs as $req) {
echo '<pre>';
print_r($req); //the last value is '22', that is wrong.
echo '</pre>';
}
}
output:
11
22
22 //it should be output this: array('id' => 123, 'name' =>
'ffff')
Expected result:
----------------
expect output result:
11
22
Array
(
[id] => 123
[name] => ffff
)
Actual result:
--------------
11
22
22
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72946&edit=1