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.