求大神帮忙解决这个数组合并问题
程序员文章站
2022-03-16 12:54:39
...
问题是把下面两个二维数组合并,根据他们相同的ID合并成一个数组
结果大概是想要这个样子的
$arr = array( array('id'=>1,'name'=>'zhangsan','sex'=>'n'), array('id'=>2,'name'=>'lisi','sex'=>'v'), array('id'=>3,'name'=>'wangwu','sex'=>'n'), array('id'=>4,'name'=>'zhaoliu','sex'=>'v'), array('id'=>5,'name'=>'xiaoqi','sex'=>'n'));$arr1 = array( array('id'=>3,'age'=>'20'), array('id'=>5,'age'=>'21'), array('id'=>6,'age'=>'30'));
结果大概是想要这个样子的
$arr = array( array('id'=>3,'name'=>'wangwu','sex'=>'n','age'=>'20'), array('id'=>5,'name'=>'xiaoqi','sex'=>'n','age'=>'21'));
回复讨论(解决方案)
好吧自己弄好了
$bbs = array(); foreach ($arr as $k => $v) { foreach ($arr1 as $key => $value) { if($value['id']==$v['id']){ $bbs[] = array_merge($arr[$k],$arr1[$key]); } } }
array_merge_recursive
foreach ( $arr as $v ) {
$tmp[$v['id']] = $v;
}
foreach ( $arr1 as $v ) {
$tmp[$v['id']] = $v;
}
print_r($tmp);