|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48854 array_merge_recursive modifies arrays after first one
Submitted: 2009-07-08 15:43 UTC Modified: 2009-07-09 01:58 UTC
From: stakadush at yahoo dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 5.3.0 OS: Ubuntu 9.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: stakadush at yahoo dot com
New email:
PHP Version: OS:


 [2009-07-08 15:43 UTC] stakadush at yahoo dot com
array_merge_recursive seems to modify the arrays which are passed, 
except for the first one.
it turns all first-level non-array elements, into arrays.
the code will explain it better :)

Reproduce code:
$array1 = array(
	'friends' => 5,
	'children' => array(
		'dogs' => 0,

$array2 = array(
	'friends' => 10,
	'children' => array(
		'cats' => 5,

echo sprintf("Second array before: %s\n", print_r($array2, true));
$merged = array_merge_recursive($array1, $array2);
echo sprintf("Second array after: %s\n", print_r($array2, true));

Expected result:
to have $array2 untouched after array_merge_recursive:

    [friends] => 10
    [children] => Array
            [cats] => 5


Actual result:
$array2 gets modified after array_merge_recursive, and every first-
level no-array element, gets turned into an array (in this case 

    [friends] => Array
            [0] => 10

    [children] => Array
            [cats] => 5



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-08 18:23 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Fixed in 5.3, HEAD.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Sep 19 09:03:37 2021 UTC