简体   繁体   English

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

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

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. 我曾尝试使用array_merge ,它可以像我想的那样进行覆盖,但如果用户指定了默认设置中不存在的设置,它还会添加新设置。 Is there a better function I can use for this than array_merge ? 有没有比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
)

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 . 键/值(和顺序)从第一选择$user在加法,这就是为什么b到来之前a阵列中,没有a$user Any keys not defined in $user that are defined in $default will then be added to the end of $user . $user 定义的任何未在$default 定义的键将被添加到$user的末尾。 Then you remove any keys in $user + $default that aren't defined in $default . 然后删除$user + $default中未在$default定义的任何键。

It's probably simplest to just loop over the keys in the default-settings array, if you only want to consider those. 如果你只想考虑那些,那么在default-settings数组中循环键可能是最简单的。 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;
    }
}
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. 我想这就是你想要的。

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

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM