php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76511 array_merge_recursive() treats root indexed array keys differently than sub-arr
Submitted: 2018-06-21 12:32 UTC Modified: 2018-06-21 20:54 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: german dot drulyk at gmail dot com Assigned:
Status: Verified Package: Arrays related
PHP Version: Irrelevant OS: https://3v4l.org/
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2018-06-21 12:32 UTC] german dot drulyk at gmail dot com
Description:
------------
I am not sure which behavior is the bug but the root array gets re-indexed to 0 and 1 and the array at position "k" goes from 4 to 5.

array_merge() exhibits the same behavior with the root array.

Test script:
---------------
$arr1 = array(
    4 => 'a',
    'k' => array( 4 => 'a' )
);

$arr2 = array(
    5 => 'b',
    'k' => array( 5 => 'b' )
);

$arr3 = array_merge_recursive( $arr1, $arr2 );

var_dump( $arr3 );

Expected result:
----------------
array(3) {
  [0]=>           // Should this be 4?
  string(1) "a"
  ["k"]=>
  array(2) {
    [4]=>         // or should this be 0?
    string(1) "a"
    [5]=>         // or should this be 1?
    string(1) "b"
  }
  [1]=>           // Should this be 5?
  string(1) "b"
}

Actual result:
--------------
array(3) {
  [0]=>
  string(1) "a"
  ["k"]=>
  array(2) {
    [4]=>
    string(1) "a"
    [5]=>
    string(1) "b"
  }
  [1]=>
  string(1) "b"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-21 12:54 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2018-06-21 12:54 UTC] cmb@php.net
For reference: <https://3v4l.org/qpnVs>.

The docs on array_merge() are pretty clear in this regard[1]:

| If, however, the arrays contain numeric keys, the later value
| will not overwrite the original value, but will be appended.
|
| Values in the input array with numeric keys will be renumbered
| with incrementing keys starting from zero in the result array.

The latter info is missing from the array_merge_recursive() docs,
though, wrt. the toplevel array.

[1] <http://php.net/manual/en/function.array-merge.php>
 [2018-06-21 13:23 UTC] german dot drulyk at gmail dot com
I am not criticizing the decision to renumber the root array to start at zero.

The purpose of this report is to show the array_merge_recursive() treats sub-array indexes differently than the root array.

To clarify:

Why did the sub-array keys retain 4 and 5 even though the root array went 0 and 1

This seems to be inconsistent behavior.
 [2018-06-21 20:54 UTC] cmb@php.net
> Why did the sub-array keys retain 4 and 5 even though the root
> array went 0 and 1
>
> This seems to be inconsistent behavior

I have to agree.  However, the functions behave this way since PHP
4.3 at least, so simply changing the behavior would cause subtle
BC breaks, and should not be done without discussion on the
internals@ mailing list and likely an RFC.  Feel free to start
the process[1]. :)

[1] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 02:01:28 2024 UTC