php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72946 variable assignment failure with foreach after reference
Submitted: 2016-08-26 09:28 UTC Modified: 2016-09-04 04:22 UTC
From: shaveneLee at gmail dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 7.0.10 OS: Windows 10 or Centos 6.8
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: shaveneLee at gmail dot com
New email:
PHP Version: OS:

 

 [2016-08-26 09:28 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-26 10:06 UTC] laruence@php.net
from https://3v4l.org/CafJM ,  I don't see the problem, do you have any thirdpart extension loaded which may affect the behavior?
 [2016-08-26 12:39 UTC] cmb@php.net
-Status: Open +Status: Feedback
 [2016-09-04 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC