php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61299 array_merge() does only merge references of objects
Submitted: 2012-03-06 10:58 UTC Modified: 2012-03-06 11:17 UTC
From: simon dot sattes at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3.10 OS: Debian Squeeze, 64-bit
Private report: No CVE-ID: None
 [2012-03-06 10:58 UTC] simon dot sattes at gmail dot com
Description:
------------
I'm not sure, if this is a bug or a desired behaviour that is not mentioned in the docs. When merging an array with itself with array_merge() then modifiying one array element afterwards does also affect all other elements.

If it's intentional, then it should be mentionend in the documentation.

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

$items = array();

$items[] = (object) array(
	'year' => 2012,
	'desc' => 'something happens'
);

$many = array_merge($items, $items);

print_r($many);

$many[0]->year = 2040;

print_r($many); // changes also year of $many[1]

Expected result:
----------------
$many[0]->year should be 2040
$many[1]->year should be 2012

Actual result:
--------------
$many[0]->year is 2040
$many[1]->year is 2040

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-06 11:17 UTC] cataphract@php.net
-Status: Open +Status: Not a bug
 [2012-03-06 11:17 UTC] cataphract@php.net
No bug here. Object variables have an extra level of indirection (they're object references). This is the general behavior of the language and doesn't require a special mention in array_merge.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 18:01:34 2024 UTC