简体   繁体   English

如何通过仅接管与第一个具有相同键的第二个数组中的值来合并两个 arrays?

[英]How to merge two arrays by taking over only values from the second array that has the same keys as the first one?

I'd like to merge two arrays with each other:我想将两个 arrays 相互合并:

$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');

Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered :而合并应包括$filtered的所有元素以及$changed中在$filtered中具有相应键的所有元素:

$merged = array(1 => 'a', 3 => 'c*');

array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. array_merge($filtered, $changed)也会将 $ $changed的附加键添加到$filtered中。 So it does not really fit.所以它真的不适合。

I know that I can use $keys = array_intersect_key($filtered, $changed) to get the keys that exist in both arrays which is already half of the work.我知道我可以使用$keys = array_intersect_key($filtered, $changed)来获取 arrays 中存在的密钥,这已经是工作的一半。

However I'm wondering if there is any (native) function that can reduce the $changed array into an array with the $keys specified by array_intersect_key ?但是我想知道是否有任何(本机) function 可以将$changed数组减少为具有由array_intersect_key指定的$keys的数组? I know I can use array_filter with a callback function and check against $keys therein, but there is probably some other purely native function to extract only those elements from an array of which the keys can be specified?我知道我可以使用带有回调array_filter的 array_filter 并检查其中的$keys ,但可能还有其他一些纯原生的 function 仅从可以指定键的数组中提取那些元素?

I'm asking because the native functions are often much faster than array_filter with a callback.我问是因为本机函数通常比带有回调的array_filter快得多。

This should do it, if I'm understanding your logic correctly:如果我正确理解您的逻辑,这应该可以做到:

array_intersect_key($changed, $filtered) + $filtered

Implementation:执行:

$filtered = array(1 => 'a', 3 => 'c');
$changed = array(2 => 'b*', 3 => 'c*');
$expected = array(1 => 'a', 3 => 'c*');    
$actual = array_key_merge_deceze($filtered, $changed);

var_dump($expected, $actual);

function array_key_merge_deceze($filtered, $changed) {
    $merged = array_intersect_key($changed, $filtered) + $filtered;
    ksort($merged);
    return $merged;
}

Output: Output:

Expected:
array(2) {
  [1]=>
  string(1) "a"
  [3]=>
  string(2) "c*"
}

Actual:
array(2) {
  [1]=>
  string(1) "a"
  [3]=>
  string(2) "c*"
}

if you want the second array ($b) to be the pattern that indicates that if there is only the key there, then you could also try this如果您希望第二个数组 ($b) 成为指示如果那里只有键的模式,那么您也可以试试这个

$new_array =  array_intersect_key( $filtered, $changed ) + $changed;

If your keys are non-numeric (which yours are not, so this is not a solution to your exact question), then you can use this technique:如果您的键是非数字的(您的不是,所以这不是您确切问题的解决方案),那么您可以使用这种技术:

$filtered = array('a' => 'a', 'c' => 'c');
$changed = array('b' => 'b*', 'c' => 'c*');

$merged = array_slice(array_merge($filtered, $changed), 0, count($filtered));

Result:结果:

Array
(
    [a] => a
    [c] => c*
)

This works because for non-numeric keys, array_merge overwrites values for existing keys, and appends the keys in $changed to the end of the new array.这是因为对于非数字键, array_merge会覆盖现有键的值,并将$changed中的键附加到新数组的末尾。 So we can simply discard any keys from the end of the merged array more than the count of the original array.所以我们可以简单地丢弃合并数组末尾的任何键,而不是原始数组的计数。

Since this applies to the same question but with different key types I thought I'd provide it.由于这适用于相同的问题,但具有不同的密钥类型,我想我会提供它。

If you use this with numeric keys then the result is simply the original array ( $filtered in this case) with re-indexed keys (IE as if you used array_values ).如果您将其与数字键一起使用,则结果只是带有重新索引键的原始数组(在这种情况下$filtered )(IE 就像您使用array_values一样)。

暂无
暂无

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

相关问题 合并两个数组,将第一个数组值保留为键,将第二个数组值保留为值 - Merge two arrays keeping the first array values as keys and second array values as values 比较两个数组中公用的键值后,如何将第二个数组的某些字段合并到第一个数组中? - How to merge some fields of second array into first array after comparing the values of keys common in both arrays? 如何将两个数组的第二级数值与不同数量的键合并? - How to merge values of the second level of two arrays with different number of keys? 合并两个平面 arrays 并在另一个数组中出现更多相同值时忽略一个数组中的值 - Merge two flat arrays and omit values from one array when the other array has more occurrences of the same value 将两个 object arrays 合并为一个 object 或带有键和值的数组 - Merge two object arrays into one object or array with keys and values 在 PHP 中合并 2 个数组,只保留第一个数组中的键 - Merge 2 arrays in PHP, keeping only keys from first array 如何仅对第一个数组中的键递归合并两个数组 - How can I merge two arrays recursively for only keys in the first array 如何将3个数组合并为一个大数组(相同的键) - How to merge 3 arrays into one big array (same keys) 使用相同的键将2个阵列合并为一个阵列 - Merge 2 arrays into one array with same keys 将两个数组合并为一个数组(相同的键) - Merge two arrays into one array (identic keys)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM