恳请帮忙,php比较两个二维数组并按键排序查出重复值。。

WBOY
Release: 2016-06-23 14:21:59
Original
907 people have browsed it

二维数组 php 排序

示例如下:
$arr1=array(   0=>array('pid'=>1,name=>'user1'),   1=>array('pid'=>2,name=>'user2'));
Copy after login

$arr2=array(   0=>array('pid'=>3,name=>'user1'),   1=>array('pid'=>4,name=>'user4'));
Copy after login

我要得到下面这样的结果:
$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'));
Copy after login

我要比较这两个数组并将重复值查出来按照pid排序,然后把pid大的那个重复user1改成user3


回复讨论(解决方案)

$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);
Copy after login
Copy after login
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        ))
Copy after login
Copy after login

我要比较这两个数组并将 重复值查出来按照pid排序,然后把pid大的那个重复user1改成user3
你的arr1和arr2有重复的数据?什么样的才算重复?name一样就是重复?


我要比较这两个数组并将 重复值查出来按照pid排序,然后把pid大的那个重复user1改成user3
你的arr1和arr2有重复的数据?什么样的才算重复?name一样就是重复?
嗯。

$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);
Copy after login
Copy after login
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        ))
Copy after login
Copy after login


xu大可否给我讲解下这个$t是以什么样的形式出现的?他是怎么存储的呢?

版主威望,他是以name作为key,重新组织了一个数组叫$t,每次创建一行数据,都是检查$t是否存在这个key.存在就判断是否是最大的pid。不存在就完整的作$t的一行数据。

你不是已经看到打印的结果了吗?
由于你要求 name 不重复,因此我们就可以用 name 做关联键
当发现关联键可能会重复的时候就做相应的处理

你不是已经看到打印的结果了吗?
由于你要求 name 不重复,因此我们就可以用 name 做关联键
当发现关联键可能会重复的时候就做相应的处理
谢谢哦

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template