php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76505 array_merge_recursive() is duplicating sub-array keys
Submitted: 2018-06-20 13:29 UTC Modified: 2018-06-20 14:30 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: german dot drulyk at gmail dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 7.2.6 OS: https://3v4l.org/
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: german dot drulyk at gmail dot com
New email:
PHP Version: OS:

 

 [2018-06-20 13:29 UTC] german dot drulyk at gmail dot com
Description:
------------
Keys are duplicated in the sub-array.

Test script:
---------------
<?php
$array1 = array(
    'k' => array(
        2 => 100,
        98 => 200,
    )
);

$array2 = array(
    'k' => array(
        64 => 300
    )
);

$array3 = array_merge_recursive( $array1, $array2 );

echo '<pre>';
var_dump( $array3 );

echo 'count: '.count( $array3['k'] );

Expected result:
----------------
array(1) {
  ["k"]=>
  array(3) {
    [2]=>
    int(100)
    [98]=>
    int(200)
    [99]=>
    int(300)
  }
}
count: 3

Actual result:
--------------
array(1) {
  ["k"]=>
  array(3) {
    [2]=>
    int(100)
    [98]=>
    int(200)
    [2]=>
    int(300)
  }
}
count: 3

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-20 13:30 UTC] german dot drulyk at gmail dot com
PHP 4.x and 5.x produce the expected result but PHP 7.x does not.
 [2018-06-20 14:16 UTC] requinix@php.net
-Status: Open +Status: Verified
 [2018-06-20 14:16 UTC] requinix@php.net
https://3v4l.org/6ivO9 Bizarre.

But shouldn't the expected result have 64=>300 and not 99=>300?
 [2018-06-20 14:21 UTC] requinix@php.net
> But shouldn't the expected result have 64=>300 and not 99=>300?
Actually no. The docs aren't exactly clear on this but I believe it's intended behavior that values in the second array having numeric keys are appended rather than created using the same key.
 [2018-06-20 14:21 UTC] cmb@php.net
> But shouldn't the expected result have 64=>300 and not 99=>300?

The docs say[1]:

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

[1] <http://php.net/manual/en/function.array-merge-recursive.php>
 [2018-06-20 14:30 UTC] requinix@php.net
> | If, however, the arrays have the same numeric key, the later
> | value will not overwrite the original value, but will be appended.
Right, but here they aren't "the same numeric key". The 64 only exists in the second array.

The truth is that numeric keys' values are always appended, regardless of whether they existed in the first array.
 [2018-06-20 15:36 UTC] german dot drulyk at gmail dot com
I see some discussion centered around the expected result and just wanted to chime in.

Numeric keys in the root array are treated differently than sub-arrays.
 [2018-06-21 09:27 UTC] laruence@php.net
Automatic comment on behalf of laruence@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6531719da575e8ceb14fa8e9fefcac2ab365dd00
Log: Fixed bug #76505 (array_merge_recursive() is duplicating sub-array keys)
 [2018-06-21 09:27 UTC] laruence@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Oct 16 05:01:27 2024 UTC