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@02405.com'
1 =>
array (size=2)
'id' => string '2'
'email' => string '2@02405.com'
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@02405.com'
'name' => string '李四'
1 =>
array (size=3)
'id' => string '2'
'email' => string '2@02405.com'
'name' => string '张三'