Bug #76505 [Ver]: array_merge_recursive() is duplicating sub-array keys

From: Date: Wed, 20 Jun 2018 14:30:19 +0000
Subject: Bug #76505 [Ver]: array_merge_recursive() is duplicating sub-array keys
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-215814@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76505&edit=1 ID: 76505 Updated by: requinix@php.net Reported by: german dot drulyk at gmail dot com Summary: array_merge_recursive() is duplicating sub-array keys Status: Verified Type: Bug Package: Arrays related Operating System: https://3v4l.org/ PHP Version: 7.2.6 Block user comment: N Private report: N New Comment: > | 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. Previous Comments: ------------------------------------------------------------------------ [2018-06-20 14:21:57] 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:21:10] 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:16:52] requinix@php.net https://3v4l.org/6ivO9 Bizarre. But shouldn't the expected result have 64=>300 and not 99=>300? ------------------------------------------------------------------------ [2018-06-20 13:30:48] 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 13:29:36] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76505&edit=1

« previous php.bugs (#215814) next »