PHP:如何在一个数组中覆盖另一个数组中的值,而不向数组中添加新键?

时间:2021-09-18 01:38:58

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?

我尝试使用array_merge,它可以像我想要的那样进行重写,但是如果用户指定了默认设置中不存在的设置,它还会添加新的设置。有比array_merge更好的函数吗?或者,我是否可以使用一个函数来过滤用户指定的数组,以便它只包含默认设置数组中也存在的键?

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
)

4 个解决方案

#1


21  

You can actually just add two arrays together ($user+$default) instead of using array_merge.

实际上,您可以添加两个数组($user+$default),而不是使用array_merge。

If you want to stop any user settings that don't exist in the defaults you can use array_intersect_key:

如果您想停止默认设置中不存在的任何用户设置,可以使用array_intersect_key:

Returns an associative array containing all the entries of array1 which have keys that are present in all arguments

返回一个关联数组,该数组包含array1的所有条目,这些条目具有在所有参数中都存在的键

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.

键/值(和顺序)首先从$user中选择,这就是为什么b在数组a之前,$user中没有a。$user中未定义的任何键都将被添加到$user的末尾。然后,删除$user + $default中未定义为$default的任何键。

#2


1  

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:

如果您只想考虑这些,那么可能最简单的方法就是对default-settings数组中的键进行循环。你可以这样做:

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;
    }
}

#3


1  

foreach($default as $key=>$val){   
  if (isset($user[$key]))
  {
    $settings[$key] = $user[$key];
  } else {
    $settings[$key] = $default[$key];
  } 
}

I think this is what you want.

我想这就是你想要的。

#4


0  

foreach($user_settings as $key=>$val){   
    $global_settings[$key] = $val; 
}

?

吗?

#1


21  

You can actually just add two arrays together ($user+$default) instead of using array_merge.

实际上,您可以添加两个数组($user+$default),而不是使用array_merge。

If you want to stop any user settings that don't exist in the defaults you can use array_intersect_key:

如果您想停止默认设置中不存在的任何用户设置,可以使用array_intersect_key:

Returns an associative array containing all the entries of array1 which have keys that are present in all arguments

返回一个关联数组,该数组包含array1的所有条目,这些条目具有在所有参数中都存在的键

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.

键/值(和顺序)首先从$user中选择,这就是为什么b在数组a之前,$user中没有a。$user中未定义的任何键都将被添加到$user的末尾。然后,删除$user + $default中未定义为$default的任何键。

#2


1  

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:

如果您只想考虑这些,那么可能最简单的方法就是对default-settings数组中的键进行循环。你可以这样做:

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;
    }
}

#3


1  

foreach($default as $key=>$val){   
  if (isset($user[$key]))
  {
    $settings[$key] = $user[$key];
  } else {
    $settings[$key] = $default[$key];
  } 
}

I think this is what you want.

我想这就是你想要的。

#4


0  

foreach($user_settings as $key=>$val){   
    $global_settings[$key] = $val; 
}

?

吗?