恳请帮忙,php比较两个二维数组并按键排序查出重复值。。
示例如下:
$arr1=array(
0=>array('pid'=>1,name=>'user1'),
1=>array('pid'=>2,name=>'user2')
);
$arr2=array(
0=>array('pid'=>3,name=>'user1'),
1=>array('pid'=>4,name=>'user4')
);
$arr2=array(
0=>array('pid'=>1,name=>'user1'),
1=>array('pid'=>2,name=>'user2')
2=>array('pid'=>3,name=>'user3'),
3=>array('pid'=>4,name=>'user4')
);
$arr1=array(
0=>array('pid'=>1,'name'=>'user1'),
1=>array('pid'=>2,'name'=>'user2')
);
$arr2=array(
0=>array('pid'=>3,'name'=>'user1'),
1=>array('pid'=>4,'name'=>'user4')
);
$t = array();
foreach(array_merge($arr1, $arr2) as $v) {
if(isset($t[$v['name']])) {
if($t[$v['name']]['pid'] > $v['pid']) $t[$v['name']]['name'] = "user{$v['pid']}";
else $v['name'] = "user{$v['pid']}";
}
$t[$v['name']] = $v;
}
print_r($t);
Array
(
[user1] => Array
(
[pid] => 1
[name] => user1
)
[user2] => Array
(
[pid] => 2
[name] => user2
)
[user3] => Array
(
[pid] => 3
[name] => user3
)
[user4] => Array
(
[pid] => 4
[name] => user4
)
)