php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54943 Array_merge links resulting array to source arrays by reference.
Submitted: 2011-05-27 19:41 UTC Modified: 2011-06-07 20:43 UTC
From: dtrenz at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.6 OS: Mac OSX
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: dtrenz at gmail dot com
New email:
PHP Version: OS:

 

 [2011-05-27 19:41 UTC] dtrenz at gmail dot com
Description:
------------
When using array_merge() on an array of objects, the resulting array is linked to 
the source array(s) as if passed by reference so that modifying the resulting 
array after the merge ALSO modifies the source array.

Test script:
---------------
<?php

$obj = (object) null;
$obj->name = 'Bob';

$arrA = array();
$arrB = array($obj); // name = "Bob"

$arrA = array_merge($arrA, $arrB);

// Change name property of first element
$arrA[0]->name = 'Joe';

print_r($arrA);  // name = "Joe"
print_r($arrB);  // name = "Joe"; source array was modified, should still be "Bob

Expected result:
----------------
Array
(
    [0] => stdClass Object
        (
            [name] => Joe
        )
 
)

Array
(
    [0] => stdClass Object
        (
            [name] => Bob
        )
 
)

Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [name] => Joe
        )
 
)

Array
(
    [0] => stdClass Object
        (
            [name] => Joe
        )
 
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-07 20:43 UTC] dsp@php.net
-Status: Open +Status: Bogus
 [2011-06-07 20:43 UTC] dsp@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

they both hold the same reference:

http://www.php.net/manual/en/language.oop5.references.php
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Sep 15 05:00:01 2025 UTC