Php array deduplication and analysis. Duplicate items in one-dimensional array: Use the array_unique function. use the following example: Copy the code as follows :? Php $ aaarray (apple, banana, pear, apple, wail, watermalon); $
Repeated items in one-dimensional array:
Use the array_unique function. use the following example:
The code is as follows:
$ Aa = array ("apple", "banana", "pear", "apple", "wail", "watermalon ");
$ Bb = array_unique ($ aa );
Print_r ($ bb );
?>
The result is as follows: Array ([0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon ).
Repeated items of two-dimensional arrays:
Two-dimensional arrays are discussed in two cases. one is because the values of one-key names cannot be repeated and repeated items are deleted. The other is because the values of one-dimensional arrays cannot be identical, the following example shows how to delete duplicate items:
(I) because the values of one-click names cannot be repeated, duplicate items are deleted.
The code is as follows:
Function assoc_unique ($ arr, $ key)
{
$ Tmp_arr = array ();
Foreach ($ arr as $ k => $ v)
{
If (in_array ($ v [$ key], $ tmp_arr) // search for $ v [$ key] in the $ tmp_arr array. if yes, true is returned.
{
Unset ($ arr [$ k]);
}
Else {
$ Tmp_arr [] = $ v [$ key];
}
}
Sort ($ arr); // sort the array
Return $ arr;
}
$ Aa = array (
Array ('id' => 123, 'name' => 'Zhang San '),
Array ('id' => 123, 'name' => 'Lily '),
Array ('id' => 124, 'name' => 'Wang Wu '),
Array ('id' => 125, 'name' => 'Zhao Liu '),
Array ('id' => 126, 'name' => 'Zhao Liu ')
);
$ Key = 'id ';
Assoc_unique (& $ aa, $ key );
Print_r ($ aa );
?>
The result is: Array ([0] => Array ([id] => 123 [name] => Zhang San) [1] => Array ([id] => 124 [name] => Wang Wu) [2] => Array ([id] => 125 [name] => Zhao 6) [3] => Array ([id] => 126 [name] => Zhao 6 ))
(Ii) because the internal one-dimensional arrays cannot be completely the same, the duplicate items are deleted.
The code is as follows:
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;
}
$ Aa = array (
Array ('id' => 123, 'name' => 'Zhang San '),
Array ('id' => 123, 'name' => 'Lily '),
Array ('id' => 124, 'name' => 'Wang Wu '),
Array ('id' => 123, 'name' => 'Lily '),
Array ('id' => 126, 'name' => 'Zhao Liu ')
);
$ Bb = array_unique_fb ($ aa );
Print_r ($ bb)
?>
Display result: Array ([0] => Array ([0] => 123 [1] => Zhang San) [1] => Array ([0] => 123 [1] => Li Si) [2] => Array ([0] => 124 [1] => Wang Wu) [4] => Array ([0] => 126 [1] => Zhao 6 ))
The array_unique function can be used by using the following example :? Php $ aa = array ("apple", "banana", "pear", "apple", "wail", "watermalon"); $...