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
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Thu Apr 25 21:01:36 2024 UTC