Analysis on de-duplication of two-dimensional arrays in PHP

Source: Internet
Author: User

Previously I wrote an article about array deduplication, but it is limited to one-dimensional arrays. The following functions can be used for two-dimensional arrays:
Copy codeThe Code is as follows:
// Remove duplicate values from two-dimensional arrays
Function array_unique_fb ($ array2D)
{
Foreach ($ array2D as $ v)
{
$ V = join (",", $ v); // dimension reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [] = $ v;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v)
{
$ Temp [$ k] = explode (",", $ v); // re-assemble the split Array
}
Return $ temp;
}

If you want to retain the key value of the array, you can use the following function:
Copy codeThe Code is as follows:
// Remove duplicate values from the two-dimensional array and retain the key value
Function array_unique_fb ($ array2D)
{
Foreach ($ array2D as $ k => $ v)
{
$ V = join (",", $ v); // dimension reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
$ Temp [$ k] = $ v;
}
$ Temp = array_unique ($ temp); // remove the duplicate string, that is, the duplicate one-dimensional array.
Foreach ($ temp as $ k => $ v)
{
$ Array = explode (",", $ v); // re-assemble the split array
$ Temp2 [$ k] ["id"] = $ array [0];
$ Temp2 [$ k] ["litpic"] = $ array [1];
$ Temp2 [$ k] ["title"] = $ array [2];
$ Temp2 [$ k] ["address"] = $ array [3];
$ Temp2 [$ k] ["starttime"] = $ array [4];
$ Temp2 [$ k] ["endtime"] = $ array [5];
$ Temp2 [$ k] ["classid"] = $ array [6];
$ Temp2 [$ k] ["ename"] = $ array [7];
}
Return $ temp2;
}

This is probably the case.
Two-dimensional array deduplication
Copy codeThe Code is as follows:
<? Php
$ Arr = array (
Array ('id' => 1, 'name' => 'aaa '),
Array ('id' => 2, 'name' => 'bbb '),
Array ('id' => 3, 'name' => 'ccc '),
Array ('id' => 4, 'name' => 'ddd '),
Array ('id' => 5, 'name' => 'ccc '),
Array ('id' => 6, 'name' => 'aaa '),
Array ('id' => 7, 'name' => 'bbb '),
);
Function assoc_unique (& $ arr, $ key)
{
$ RAr = array ();
For ($ I = 0; $ I <count ($ arr); $ I ++)
{
If (! Isset ($ rAr [$ arr [$ I] [$ key])
{
$ RAr [$ arr [$ I] [$ key] = $ arr [$ I];
}
}
$ Arr = array_values ($ rAr );
}
Assoc_unique (& $ arr, 'name ');
Print_r ($ arr );
?>

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.