|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-02-05 14:49 UTC] tomas dot matousek at matfyz dot cz
Description:
------------
Function array_merge_recursive merges arrays recursivelly but the behavior of merging is different in the first level from the next ones.
Merging of arrays in the second and next levels starts key indexing from the key of the first item merged but on the first level it starts always from zero.
Maybe it is a feature but IMHO it would be better to implement the same behavior on each level of a recursion.
Reproduce code:
---------------
$ar1 = array(4=>4, 5=>5);
$ar2 = array(4=>4, 5=>5);
print_r(array_merge_recursive($ar1, $ar2));
$ar1 = array("a" => array(4=>4, 5=>5));
$ar2 = array("a" => array(4=>4, 5=>5));
print_r(array_merge_recursive($ar1, $ar2));
$ar1 = array("a" => array("a" => array(4=>4, 5=>5)));
$ar2 = array("a" => array("a" => array(4=>4, 5=>5)));
print_r(array_merge_recursive($ar1, $ar2));
Expected result:
----------------
Array
(
[0] => 4
[1] => 5
[2] => 4
[3] => 5
)
Array
(
[a] => Array
(
[0] => 4
[1] => 5
[2] => 4
[3] => 5
)
)
Array
(
[a] => Array
(
[a] => Array
(
[0] => 4
[1] => 5
[2] => 4
[3] => 5
)
)
)
Actual result:
--------------
Array
(
[0] => 4
[1] => 5
[2] => 4
[3] => 5
)
Array
(
[a] => Array
(
[4] => 4
[5] => 5
[6] => 4
[7] => 5
)
)
Array
(
[a] => Array
(
[a] => Array
(
[4] => 4
[5] => 5
[6] => 4
[7] => 5
)
)
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 17 18:00:01 2025 UTC |
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.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 ) )