PHP根据键值相同合并两个二维数组:


function array_merge_by_same_key_value($array1,$array2,$key){
    $array = [];
    foreach($array1 as $k1=>$v1){
        foreach($array2 as $k2=>$v2){
            if($v1[$key] == $v2[$key]){
                $array[$k1]=array_merge($v1,$v2);
            }
        }
    }
    return $array;
}

示例:


array1 (size=2)
  0 =>
    array (size=2)
      'id' => string '1'
      'email' => string '1@www.codesou.cn'
  1 =>
    array (size=2)
      'id' => string '2'
      'email' => string '2@www.codesou.cn'

 


array2 (size=2)
  0 =>
    array
      'id' => string '2'
      'name' => string '张三'
  1 =>
    array (size=2)
      'id' => string '1'
      'name' => string '李四'

输出:


array1 (size=2)
  0 =>
    array (size=3)
      'id' => string '1'
      'email' => string '1@www.codesou.cn'
      'name' => string '李四'
  1 =>
    array (size=3)
      'id' => string '2'
      'email' => string '2@www.codesou.cn'
      'name' => string '张三'