PHP two-dimensional array to remove specific duplicate values

Source: Internet
Author: User
[Help] PHP two-dimensional array to remove specific duplicate values original array: $ arr & nbsp; array (id & nbsp; & gt; & nbsp; 1, name & nbsp; & gt; & nbsp; aaa, uid & gt; 1), & nbsp; array (id & nbsp; & gt; & nbsp; 2, name & nbsp; & [help] PHP two-dimensional array to remove specific duplicate values
Original array:
$ Arr = array (
Array ('id' => 1, 'name' => 'AAA', 'uid' => 1 ),
Array ('id' => 2, 'name' => 'BBB ', 'uid' => 2 ),
Array ('id' => 3, 'name' => 'CCC ', 'uid' => 3 ),
Array ('id' => 4, 'name' => 'ddd ', 'uid' => 4 ),
Array ('id' => 5, 'name' => 'CCC ', 'uid' => 3 ),
Array ('id' => 6, 'name' => 'BBB ', 'uid' => 2 ),
Array ('id' => 7, 'name' => 'BBB ', 'uid' => 2 ),
Array ('id' => 8, 'name' => 'fff', 'uid' => 6 ),
Array ('id' => 9, 'name' => 'CCC ', 'uid' => 3 ),
Array ('id' => 10, 'name' => 'BBB ', 'uid' => 2 ),
Array ('id' => 11, 'name' => 'ddd ', 'uid' => 4 ),
Array ('id' => 12, 'name' => 'Eee ', 'uid' => 5 ),
Array ('id' => 13, 'name' => 'fff', 'uid' => 6 ),
);

Problem Description: to obtain the values of id, and 9 in $ arr, but the values of 'name' and 'uid' cannot be the same. Example: name = ccc for id = 3, but name = ccc for id = 5, take the next value of id = 6. if id = 6 is the same as the previous one, take the next one, and so on.

If you want to get the value of id 1, 3, 5, 7, 9, the expected result is:
$ Arr_new = array (
Array ('id' => 1, 'name' => 'AAA', 'uid' => 1 ),
Array ('id' => 3, 'name' => 'CCC ', 'uid' => 3 ),
Array ('id' => 6, 'name' => 'BBB ', 'uid' => 2 ),
Array ('id' => 8, 'name' => 'fff', 'uid' => 6 ),
);

Recursive PHP two-dimensional array values

Share :? 1, 'name '? =>? 'AAA', 'uid' => 1 ),? Array ('id '? =>? 2, 'name '? => ?... 'Data-pics = ''>


------ Solution --------------------
This is the method of algorithm processing. if there is a problem, the algorithm will traverse the data that has been obtained and obtain the next one if it is found. The purpose of this algorithm is to count the number of people and post more posts only. to solve this problem, you only need to comment out the code that traverses the data of the current query.
------ Solution --------------------
What does this mean?
$arr = array( 
array('id' => 1,'name' => 'aaa','uid'=>1),
array('id' => 2,'name' => 'bbb','uid'=>2),
array('id' => 3,'name' => 'ccc','uid'=>3),
array('id' => 4,'name' => 'ddd','uid'=>4),
array('id' => 5,'name' => 'ccc','uid'=>3),
array('id' => 6,'name' => 'bbb','uid'=>2),
array('id' => 7,'name' => 'bbb','uid'=>2),
array('id' => 8,'name' => 'fff','uid'=>6),
array('id' => 9,'name' => 'ccc','uid'=>3),
array('id' => 10,'name' => 'bbb','uid'=>2),
array('id' => 11,'name' => 'ddd','uid'=>4),
array('id' => 12,'name' => 'eee','uid'=>5),
array('id' => 13,'name' => 'fff','uid'=>6),
);
$id = array(1,3,5,7,9);

foreach($arr as $v) $a[$v['id']] = $v;

$t = array();
foreach($id as $k) {
if(! in_array($a[$k]['name'], $t)) {
$res[] = $a[$k];
$t[] = $a[$k]['name'];
}else {
for($i=$k+1; $i if(! in_array($a[$i]['name'], $t)) {
$res[] = $a[$i];
$t[] = $a[$i]['name'];
break;
}
}
}
print_r($res);
Array
(
[0] => Array
(
[Id] => 1
[Name] => aaa
[Uid] => 1
)

[1] => Array
(
[Id] => 3
[Name] => ccc
[Uid] => 3
)

[2] => Array
(
[Id] => 6
[Name] => bbb
[Uid] => 2

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.