Categories
PHP Programming

PHP: How to array_merge Without Losing Numeric Keys

PHP function array_merge is nice in that you can easily merge one (or more) arrays into a single array with a single call of the function. The keys are retained, but only if they’re not numeric keys(!).

E.g.

 "hi");
$array2 = array(3 => "data");
$result = array_merge($array1, $array2);
?>

Result:
Array
(
    [0] => hi
    [1] => data
)

Perhaps this is the behavior you want. But what if you want to retain the original numeric keys?

Advertisement