| This article introduces a method for removing duplicates of PHP arrays, including the deduplication of one-dimensional arrays and the de-weight of two-dimensional arrays. Refer to a friend who needs it. First, take a look at the removal of one-dimensional array duplicates. Use the Array_unique function, for example:
Output: Array ([0] = Apple [1] = banana [2] = pear [4] = wail [5] = Watermalon). Then we look at the method of removing two-dimensional array duplicates. For a two-dimensional array, it is considered that the value of one key name cannot be duplicated, and duplicates are deleted because the inner one-dimensional array cannot be identical. This is illustrated by specific examples. 1, because the value of a key name cannot be duplicated, delete duplicates.
$v) { if (In_array ($v [$key], $tmp _arr))//Search for $v[$key] is present in the $tmp_arr array, if present returns True { unset ($arr [$k]); } else { $tmp _arr[] = $v [$key]; } } sort ($arr),//sort function array to return $arr;} $AA = Array (array (' ID ' =&G T 123, ' name ' = ' Zhang San '), array (' id ' = = 123, ' name ' = ' John Doe '), array (' id ' = = 124, ' name ' = = ' Harry '), Array (' ID ' =& Gt The ' name ' = ' Zhao Liu '), array (' id ' = = 126, ' name ' = ' Zhao Liu ')); $key = ' id '; Assoc_unique (& $aa, $key); Print_r ($AA); by bbs.it-home.org?>
Output: Array ([0] = = Array ([id] = = 123 [Name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] =&G T Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [Name] = Zhao Liu)) 2, because an internal one-dimensional array cannot be exactly the same, and delete duplicates
$v) { $temp [$k] = Explode (",", $v); Re-assemble the disassembled array } return $temp;} $aa = 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); by bbs.it-home.org?>
Output 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)) |