Home >Backend Development >PHP Tutorial >求大神帮忙解决这个数组合并问题

求大神帮忙解决这个数组合并问题

WBOY
WBOYOriginal
2016-06-23 13:13:36905browse

问题是把下面两个二维数组合并,根据他们相同的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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn