doufen3786 2018-10-01 03:12
浏览 63
已采纳

比较两个多维数组并根据匹配键替换值

I have 2 arrays of different length:

$array1 = Array
(
    [0] => Array
        (
            ['_id'] => "Group1"
            ['M'] => 0
            ['F'] => 0
        )
    [1] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 0
            ['F'] => 0
        )
    [2] => Array
        (
            ['_id'] => "Group3"
            ['M'] => 0
            ['F'] => 0
        )
    [3] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 0
            ['F'] => 0
        )
)


$array2 = Array
(
    [0] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 180
            ['F'] => 200
        )
    [1] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 360
            ['F'] => 500
        )
)

I want to compare the values of ['_id'] in both array and if they match, I will replace the values of ['M'] and ['F'] in array1 with those from array2 based on the corresponding ['_id].

So my desired ouptput would be:

$array1 = Array
(
    [0] => Array
        (
            ['_id'] => "Group1"
            ['M'] => 0
            ['F'] => 0
        )
    [1] => Array
        (
            ['_id'] => "Group2"
            ['M'] => 180
            ['F'] => 200
        )
    [2] => Array
        (
            ['_id'] => "Group3"
            ['M'] => 360
            ['F'] => 500
        )
    [3] => Array
        (
            ['_id'] => "Group4"
            ['M'] => 0
            ['F'] => 0
        )
)

This is my code but I can't seem to get the values replaced with the new values. The values are still the same as before.

foreach ($array1 as $defArr)
{
  foreach ($array2 as $dayArr)
  {
    if($dayArr['_id'] == $defArr['_id'])
    {
      $defArr['M'] = $dayArr['M'];
      $defArr['F'] = $dayArr['F'];
    }
  }
}
  • 写回答

2条回答 默认 最新

  • douli7841 2018-10-01 03:34
    关注

    This can be a one-character change:

    foreach ($array1 as $defArr)
    

    goes to

    foreach ($array1 as &$defArr)
    #                   ^
    

    The & reference operator points to the original sub array in the foreach loop context rather than a temporary variable.

    However, it's a bit safer to use the index explicitly:

    foreach ($array1 as $i => $defArr) {
        foreach ($array2 as $j => $dayArr) {
            if ($dayArr['_id'] == $defArr['_id']) {
                $array1[$i]['M'] = $array2[$j]['M'];
                $array1[$i]['F'] = $array2[$j]['F'];
            }
        }
    }
    

    If speed is important or $array2 is large, the time complexity of your algorithm is O(n * m). I recommend hashing $array2 for fast lookups as follows (O(n)):

    $lookup = array_reduce($array2, function ($a, $e) {
        $a[$e['_id']] = $e;
        return $a;
    });
    
    foreach ($array1 as $i => $e) {
        if (array_key_exists($e['_id'], $lookup)) {
            $array1[$i]['M'] = $lookup[$e['_id']]['M'];
            $array1[$i]['F'] = $lookup[$e['_id']]['F'];
        }
    }
    

    Try it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献