php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68402 PHP reference changes last item of array
Submitted: 2014-11-11 14:20 UTC Modified: 2014-11-11 19:53 UTC
From: villeviitaharju at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.18 OS: Windows 7
Private report: No CVE-ID: None
 [2014-11-11 14:20 UTC] villeviitaharju at gmail dot com
Description:
------------
Scenario:
foreach with value as reference

function which has foreach with value as reference and returns array

last item of unmodified array changes to modified array item

namechanges doesn't change scenario.

Test script:
---------------
$bar = [["object_name" => "first"], ["object_name" => "second"]];
foreach($bar as &$b) $b["object_name"] .= " foo";

function foo($bar){
    foreach($bar as &$o) $o["object_name"] = "bar " . $o["object_name"];
    return $bar;
}

function echoObjects($bar) {
    foreach($bar as $o) echo $o["object_name"] . "<br />";
    echo "<br />";
}

echoObjects($bar);
echoObjects(foo($bar));
echoObjects($bar); //extra bar on last item

Expected result:
----------------
first foo
second foo
 
bar first foo
bar second foo
 
first foo
second foo

Actual result:
--------------
first foo
second foo

bar first foo
bar second foo

first foo
bar second foo

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-11-11 14:25 UTC] ajf@php.net
-Status: Open +Status: Not a bug
 [2014-11-11 14:25 UTC] ajf@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This isn't a bug. Because the last item of the array is a reference, it remains a reference if that array is passed to other functions.
 [2014-11-11 19:53 UTC] requinix@php.net
-Package: PHP Language Specification +Package: *General Issues
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 07:01:32 2024 UTC