PHP two-dimensional array to remove specific duplicate values of the problem

Source: Internet
Author: User
"Help" php two-dimensional array to remove a specific duplicate value problem
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 ' = ' = ', ' name ' = ' bbb ', ' uid ' =>2),
Array (' id ' = ' = ', ' name ' = ' ddd ', ' uid ' =>4),
Array (' id ' = +, ' name ' = ' eee ', ' uid ' =>5),
Array (' id ' = ' = ', ' name ' = ' fff ', ' uid ' =>6),
);

Problem described: Want to get the value of ID 1,3,5,7,9 in $arr, but the value of ' name ' and ' uid ' cannot be duplicated. Example: id==3 corresponding to the NAME==CCC, but id==5 corresponding to the NAME==CCC, so do not take the ID 5 of this, take its next id=6 value, if the value of id=6, and the front of the duplicate, then remove one, and so on.

If you want to take a value with ID 1,3,5,7,9, you want the result:
$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),
); PHP recursive values of two-dimensional arrays

Share to:


------Solution--------------------
This is the way the algorithm is handled. He will traverse the data that is currently taken and then take one if it is found--the purpose of this algorithm is to count the number of people who post more than one person posting is only one if you want to resolve the code comment that iterates through the data part of the current query
------Solution--------------------
What does that 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 ' = ' = ', ' name ' = ' bbb ', ' uid ' =>2),
Array (' id ' = ' = ', ' name ' = ' ddd ', ' uid ' =>4),
Array (' id ' = +, ' name ' = ' eee ', ' uid ' =>5),
Array (' id ' = ' = ', ' 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.