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

时间:2021-09-08 19:16:04

I'd like to merge two arrays with each other:

我想将两个数组相互合并:

$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. So it does not really fit.

array_merge($ filtered,$ changed)也会将$ changed的附加键添加到$ filtered中。所以它并不适合。

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)来获取两个数组中存在的密钥,这些密钥已经完成了一半的工作。

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? 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?

但是我想知道是否有任何(本机)函数可以将$ changed数组减少为一个数组,其中包含由array_intersect_key指定的$ keys?我知道我可以使用带有回调函数的array_filter并检查其中的$ keys,但是可能还有一些其他的纯本机函数只能从可以指定键的数组中提取那些元素?

I'm asking because the native functions are often much faster than array_filter with a callback.

我问,因为本机函数通常比带回调的array_filter快得多。

2 个解决方案

#1


14  

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:

输出:

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

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

#2


0  

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;

#1


14  

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:

输出:

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

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

#2


0  

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;