Bug #68402 [Opn->Nab]: PHP reference changes last item of array

[email protected]
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=68402&edit=1

 ID:                 68402
 Updated by:         [email protected]
 Reported by:        villeviitaharju at gmail dot com
 Summary:            PHP reference changes last item of array
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            PHP Language Specification
 Operating System:   Windows 7
 PHP Version:        5.5.18
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2014-11-11 14:20:37] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=68402&edit=1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.