|  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
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:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
7 + 39 = ?
Subscribe to this entry?

 [2018-06-21 12:32 UTC] german dot drulyk at gmail dot com
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"
  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) {
  string(1) "a"
  array(2) {
    string(1) "a"
    string(1) "b"
  string(1) "b"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-06-21 12:54 UTC]
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2018-06-21 12:54 UTC]
For reference: <>.

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] <>
 [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]
> 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] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 22 20:01:29 2024 UTC