Doc #76511 [Ver]: array_merge_recursive() treats root indexed array keys differently than sub-arr

From: Date: Thu, 21 Jun 2018 20:54:14 +0000
Subject: Doc #76511 [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-15809@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: Verified Type: Documentation Problem Package: Arrays related Operating System: https://3v4l.org/ PHP Version: Irrelevant Block user comment: N Private report: N New Comment: > 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> Previous Comments: ------------------------------------------------------------------------ [2018-06-21 13:23:41] 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 12:54:45] 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 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

« previous php.doc.bugs (#15809) next »