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 );
?>