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

How to merge two arrays by taking over only values from the second array that has the same keys as the first one?(如何通过仅从与第一个数组具有相同键的第二个数组中接管值来合并两个数组?)
本文介绍了如何通过仅从与第一个数组具有相同键的第二个数组中接管值来合并两个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想合并两个数组:

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

而合并应包括 $filtered 的所有元素和 $changed 的所有元素,这些元素在 $filtered 中有相应的键:

Whereas the merge should include all elements of $filtered and all those elements of $changed that have a corresponding key in $filtered:

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

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

array_merge($filtered, $changed) would add the additional keys of $changed into $filtered as well. So it does not really fit.

我知道我可以使用 $keys = array_intersect_key($filtered, $changed) 来获取两个数组中存在的键,这已经完成了一半的工作.

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.

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

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?

我这么问是因为原生函数通常比带有回调的 array_filter 快得多.

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

推荐答案

如果我正确理解你的逻辑,应该这样做:

This should do it, if I'm understanding your logic correctly:

array_intersect_key($changed, $filtered) + $filtered

<小时>

实施:

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

输出:

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

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

这篇关于如何通过仅从与第一个数组具有相同键的第二个数组中接管值来合并两个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)