- $aa =array ("Apple", "banana", "pear", "apple", "wail", "Watermalon");
- $BB =array_unique ($AA);
- Print_r ($BB);
- ?>
Copy CodeOutput: Array ([0] = Apple [1] = banana [2] = pear [4] = wail [5] = Watermalon). Two, php two-dimensional array of duplicates: For a two-dimensional array we can be discussed, one is because the value of a key name cannot be duplicated, delete duplicates, and the other because the inner one-dimensional array can not be identical, and delete duplicates. Example 1, because the value of a key name cannot be duplicated, delete duplicates. Code:
- function Assoc_unique ($arr, $key)
- {
- $tmp _arr = Array ();
- foreach ($arr as $k = $v)
- {
- if (In_array ($v [$key], $tmp _arr))//Search $v[$key] is present in the $tmp_arr array, if there is a return true
- {//bbs.it-home.org
- Unset ($arr [$k]);
- }
- else {
- $tmp _arr[] = $v [$key];
- }
- }
- Sort ($arr); Sort function sorting an array
- return $arr;
- }
- $AA = Array (
- Array (' id ' = = 123, ' name ' = ' Zhang San '),
- Array (' id ' = = 123, ' name ' = ' John Doe '),
- Array (' id ' = = 124, ' name ' = ' Harry '),
- Array (' id ' = ' = ', ' name ' = ' Zhao Liu '),
- Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
- );
- $key = ' id ';
- Assoc_unique (& $aa, $key);
- Print_r ($AA);
- ?>
Copy Code Output: Array ([0] = = Array ([id] = = 123 [Name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] = = Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [name] + Zhao Liu)) Example 2, since the inner one-dimensional array cannot be identical, the deletion of the heavy The complex. Code:
- function ARRAY_UNIQUE_FB ($array 2D) {
- foreach ($array 2D as $v) {
- $v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
- $temp [] = $v;
- }//Bbs.it-home.org
- $temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
- foreach ($temp as $k = = $v) {
- $temp [$k] = Explode (",", $v); Re-assemble the disassembled array
- }
- return $temp;
- }
- $AA = Array (
- Array (' id ' = = 123, ' name ' = ' Zhang San '),
- Array (' id ' = = 123, ' name ' = ' John Doe '),
- Array (' id ' = = 124, ' name ' = ' Harry '),
- Array (' id ' = = 123, ' name ' = ' John Doe '),
- Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
- );
- $BB =ARRAY_UNIQUE_FB ($AA);
- Print_r ($BB)
- ?>
Copy CodeOutput Result: Array ([0] = = Array ([0] = = 123 [1] = Zhang San) [1] = = Array ([0] = = 123 [1] = = Li four) [2] + = Array ([0 ] = 124 [1] = Harry) [4] = = Array ([0] = = 126 [1] = Zhao Liu)) |