Edit report at http://bugs.php.net/bug.php?id=54943&edit=1

 ID:                 54943
 Updated by:         d...@php.net
 Reported by:        dtrenz at gmail dot com
 Summary:            Array_merge links resulting array to source arrays
                     by reference.
-Status:             Open
+Status:             Bogus
 Type:               Bug
 Package:            *General Issues
 Operating System:   Mac OSX
 PHP Version:        5.3.6
 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

they both hold the same reference:



http://www.php.net/manual/en/language.oop5.references.php


Previous Comments:
------------------------------------------------------------------------
[2011-05-27 19:41:16] 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

        )

 

)


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



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54943&edit=1

Reply via email to