Bug #27162 [Com]: Bad int keys generated by array_merge_recursive

From: Date: Fri, 08 Jan 2016 21:06:05 +0000
Subject: Bug #27162 [Com]: Bad int keys generated by array_merge_recursive
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198533@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=27162&edit=1 ID: 27162 Comment by: fwwarr at gmail dot com Reported by: tomas dot matousek at matfyz dot cz Summary: Bad int keys generated by array_merge_recursive Status: Not a bug Type: Bug Package: Arrays related Operating System: WinXP PHP Version: 4CVS, 5CVS Block user comment: N Private report: N New Comment: I have also come across this and believe it to be a bug. As per the documentation, array_merge is supposed to re-index numeric keys from 0. One would expect array_merge_recursive to re-index numeric keys from 0 at every level of recursion, however, it only re-indexes numeric keys at the first level. A recursive function that behaves differently at different levels of recursion is objectively a bug. Maybe a simpler example will illustrate it better: Reproduce code: --------------- $ar1 = array(1 => array(2 => 3)); print_r(array_merge_recursive($ar1)); Expected result: ---------------- Array ( [0] => Array ( [0] => 3 ) ) Actual result: ---------------- Array ( [0] => Array ( [2] => 3 ) ) Previous Comments: ------------------------------------------------------------------------ [2004-02-07 22:52:39] sniper@php.net There is no bug in this. ------------------------------------------------------------------------ [2004-02-06 14:11:01] tomas dot matousek at matfyz dot cz "If the input arrays have the same string keys, then the values for these keys are merged together into an array ..." Of course that the values are merged. But the way in which are merged is IMHO wrong. Keys are reindexed starting from 0 in the first level of recursion but in the next levels reindexing is different. As one can see on the output of an example above. Why? ------------------------------------------------------------------------ [2004-02-06 10:46:48] sniper@php.net From the manual: "If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended." ------------------------------------------------------------------------ [2004-02-06 03:41:09] tomas dot matousek at matfyz dot cz Maybe the example I used was not understood well. I don't want indexing to be propagated thru array dimensions. IMHO array_merge_recursive is indexing in two different ways depending of the level of recursion. This is better example: Reproduce code: --------------- $ar1 = array(10=>10, 7=>7); $ar2 = array(10=>10, 7=>7); print_r(array_merge_recursive($ar1, $ar2)); $ar1 = array("a" => array(10=>10, 7=>7)); $ar2 = array("a" => array(10=>10, 7=>7)); print_r(array_merge_recursive($ar1, $ar2)); $ar1 = array("a" => array("a" => array(10=>10, 7=>7))); $ar2 = array("a" => array("a" => array(10=>10, 7=>7))); print_r(array_merge_recursive($ar1, $ar2)); Actual result: -------------- Array ( [0] => 10 [1] => 7 [2] => 10 [3] => 7 ) Array ( [a] => Array ( [10] => 10 [7] => 7 [11] => 10 [12] => 7 ) ) Array ( [a] => Array ( [a] => Array ( [10] => 10 [7] => 7 [11] => 10 [12] => 7 ) ) ) Expected result is the same as above. ------------------------------------------------------------------------ [2004-02-05 21:48:16] iliaa@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php This is to be expected. Indexing of any array always starts from element 0 and is not carried over across array dimensions. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=27162 -- Edit this bug report at https://bugs.php.net/bug.php?id=27162&edit=1

« previous php.bugs (#198533) next »