[英]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.