Merge two arrays, replacing values of same key

Viewed 5630

I have two arrays

First Array

(
    [0] => Array
        (
            [352] => 1
            [128] => 
            [64] => 
            [70] => 2
        )
)

Second array is like this :

Array
(
    [0] => Array
        (
            [128] => 1.5
        )
)

I want to make final array like this.(i want to store the matching into the main array in this example it is 128 -> 1.5) how can i do it.?

Array
(
    [0] => Array
        (
            [352] => 1
            [128] => 1.5
            [64] =>  
            [70] => 2
        )

)

here is my array variables:

print_r($listskilssresult);
print_r($listskilssresultmatching);
5 Answers

array_replace_recursive is the best solution!

$old_array = Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'D'));
$new_array = Array('my_index2' => Array('2' => 'Z'));
$result = array_replace_recursive($old_array, $new_array);

//Result : Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'Z'));
Related