Reputation: 4359
I have two array
$arr1 = array(
'setting_one' => 'abc',
'setting_two' => 'def',
'setting_three' => 'test'
);
$arr2 = array(
'setting_two' => 'user defined'
);
$arr3= array();
I want $array3
to look like
array(
'setting_one' => 'abc';
'setting_two' => 'user defined',
'setting_three' => 'test'
);
how can i merge two arrays into a third array? Take into a real world example that the first two arrays are settings arrays, the first array is a global array while the second array is user defined settings. The third array is a combination of the two array while favoring the second array values over the global settings arrays.
Upvotes: 2
Views: 953
Reputation: 683
$arr3 = array_merge($arr1, $arr2);
array_merge() does the overriding from the second array for you.
Upvotes: 1
Reputation: 33437
$arr3 = array_merge($arr1, $arr2);
This merges arr2 into arr1 (meaning any collision is resolved by using the value from arr2).
Note that this is not recursive and will not merge sub arrays in the manner that you probably expect.
Upvotes: 4