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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC