Array-Merge on an associative array in PHP

Viewed 21125

How can i do an array_merge on an associative array, like so:

Array 1:

$options = array (
"1567" => "test",
"1853" => "test1",
);

Array 2:

$option = array (
"none" => "N/A"
);

So i need to array_merge these two but when i do i get this (in debug):

Array
(
    [none] => N/A
    [0] => test
    [1] => test1
)
7 Answers

If arrays having same keys then use array_merge_recursive()

$array1 = array( "a" => "1" , "b" => "45" );

$array2 = array( "a" => "23" , "b" => "33" );

$newarray = array_merge_recursive($array1,$array2);

The array_merge_recursive() wont overwrite, it just makes the value as an array.

Related