|
duplicate entries for one-dimensional arrays :
Using the Array_unique function, you can use the following example:
<?php
$aa =array ("apple", "banana", "pear", "apple", "wail", "watermalon");
$BB =array_unique ($aa);
Print_r ($bb);
?>
The results are as follows: Array ([0] = Apple [1] = Banana [2] = Pear [4] = Wail [5] = watermalon).
duplicate entries for two-dimensional arrays :
For a two-dimensional array we can be discussed in one way or another because the value of one key name cannot be duplicated, and the other is because the inner one-dimensional array cannot be identical and the duplicates are deleted, the following example Illustrates:
㈠ Delete duplicates because the value of a key name cannot be duplicated
<?php
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
{
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);
?>
The result is: array ([0] = = Array ([id] = = 123 [name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] =& Gt Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [name] = Zhao Liu))
㈡ because an internal one-dimensional array cannot be exactly the same, and delete duplicates
<?php
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;
}
$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)
?>
Display Results: 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))
|