I have an array with default settings, and one array with user-specified settings. I want to merge these two arrays so that the default settings gets overwritten with the user-specified ones.
I have tried to use array_merge
, which does the overwriting like I want, but it also adds new settings if the user has specified settings that doesn't exist in the default ones. Is there a better function I can use for this than array_merge
? Or is there a function I can use to filter the user-specified array so that it only contains keys that also exist in the default settings array?
Example of what I want
$default = array('a' => 1, 'b' => 2);
$user = array('b' => 3, 'c' => 4);
// Somehow merge $user into $default so we end up with this:
Array
(
[a] => 1
[b] => 3
)
The array_replace() function replaces the values of the first array with the values from following arrays. Tip: You can assign one array to the function, or as many as you like. If a key from array1 exists in array2, values from array1 will be replaced by the values from array2.
To replace an element in an array:Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.
Given two array arr1 and arr2 and the task is to append one array to another array. Using array_merge function: This function returns a new array after merging the two arrays. $arr1 = array ( "Geeks" , "g4g" );
Just make a note of the old value, use unset to remove it from the array then add it with the new key and the old value pair. Save this answer.
You can actually just add two arrays together ($user+$default
) instead of using array_merge
.
If you want to stop any user settings that don't exist in the defaults you can use array_intersect_key
:
Returns an associative array containing all the entries of array1 which have keys that are present in all arguments
$default = array('a' => 1, 'b' => 2);
$user = array('b' => 3, 'c' => 4);
// add any settings from $default to $user, then select only the keys in both arrays
$settings = array_intersect_key($user + $default, $default);
print_r($settings);
Array
(
[b] => 3
[a] => 1
)
The keys/values (and order) are selected first from $user
in the addition, which is why b
comes before a
in the array, there is no a
in $user
. Any keys not defined in $user
that are defined in $default
will then be added to the end of $user
. Then you remove any keys in $user + $default
that aren't defined in $default
.
It's probably simplest to just loop over the keys in the default-settings array, if you only want to consider those. So you can do something like this:
foreach ($default_settings AS $key => $default_value)
{
if (array_key_exists($key, $user_settings))
{
$combined_settings[$key] = $user_settings[$key];
}
else
{
$combined_settings[$key] = $default_value;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With