Bug->Doc #76511 [Opn->Ver]: array_merge_recursive() treats root indexed array keys differently than sub-arr
| From: | cmb@php.net | Date: | Thu, 21 Jun 2018 12:54:47 +0000 |
| Subject: | Bug->Doc #76511 [Opn->Ver]: array_merge_recursive() treats root indexed array keys differently than sub-arr | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15807@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76511&edit=1
ID: 76511
Updated by: cmb@php.net
Reported by: german dot drulyk at gmail dot com
Summary: array_merge_recursive() treats root indexed array
keys differently than sub-arr
-Status: Open
+Status: Verified
-Type: Bug
+Type: Documentation Problem
Package: Arrays related
Operating System: https://3v4l.org/
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2018-06-21 12:32:20] 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"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76511&edit=1