Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How to overwrite values in one array with values from another without adding new keys to the array?

Tags:

arrays

php

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
)
like image 622
Svish Avatar asked May 14 '10 22:05

Svish


People also ask

How do you overwrite an array in PHP?

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.

How do you replace an element in an array?

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.

How add element from one array to another in PHP?

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" );

How do you change the key of an associative array?

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.


2 Answers

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

Example:

$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);

Results:

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.

like image 141
gnarf Avatar answered Sep 20 '22 03:09

gnarf


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;
    }
}
like image 39
Chad Birch Avatar answered Sep 21 '22 03:09

Chad Birch