php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42863 array_merge_recursive() increments number values
Submitted: 2007-10-05 08:00 UTC Modified: 2007-10-06 00:04 UTC
From: moloth at hotmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.4 OS: all
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: moloth at hotmail dot com
New email:
PHP Version: OS:

 

 [2007-10-05 08:00 UTC] moloth at hotmail dot com
Description:
------------
Seems to increment number values.

Reproduce code:
---------------
<pre>
<?php
$a = array('a' => array('2' => '6'));
$b = array('a' => array('2' => '6'));
$result = array_merge_recursive( $a, $b );
print_r( $result );
?>



Expected result:
----------------
Array
(
    [a] => Array
        (
            [2] => 6
        )
)


Actual result:
--------------
Array
(
    [a] => Array
        (
            [2] => 6
            [3] => 6
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-06 00:04 UTC] jani@php.net
From the manual page for array_merge_recursive:

"If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended."

Try adding this before the array_merge_recursive() call and you'll see what's happening:

var_dump($a,$b);

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC