Please help. php compares two-dimensional arrays and sorts them by pressing the buttons to find the duplicate values ..

Source: Internet
Author: User
Please help. php compares two-dimensional arrays and sorts them by pressing the buttons to find the duplicate values .. 2d array php sorting

Example:
$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'));

I want to get the following result:
$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'));

I want to compare the two arrays and check the duplicate values to sort them by pid. then I want to change the duplicate user1 with a large pid to user3.


Reply to discussion (solution)

$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        ))

I want to compare the two arrays and check the duplicate values to sort them by pid. then I want to change the duplicate user1 with a large pid to user3.
Does your arr1 and arr2 have duplicate data? What is repeated? Is the name repeated?


I want to compare the two arrays and check the duplicate values to sort them by pid. then I want to change the duplicate user1 with a large pid to user3.
Does your arr1 and arr2 have duplicate data? What is repeated? Is the name repeated?
Well.

$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        ))


Can xu tell me how this $ t came into being? How is it stored?

The moderator prestige, which uses name as the key, re-organizes an array named $ t. every time a row of data is created, it checks whether $ t exists in this key. If yes, it determines whether it is the largest pid. If the data does not exist, it is complete as a $ t data row.

Didn't you see the printed result?
Because you require no duplicate names, we can use name as the join key.
When the association key is found to be repeated, it is processed accordingly.

Didn't you see the printed result?
Because you require no duplicate names, we can use name as the join key.
When the association key is found to be repeated, it is processed accordingly.
Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.