Nonsense not much to say that directly on the code
Copy Code code as follows:
/**
* Give the array weight
* The difference from the Array_unique function: it requires Val to be a string, and this can be an array/object
*
* @param unknown_type $arr to row the weight of the array
* @param unknown_type $reserveKey keep the original key
* @return Unknown
*/
static function M_arrayunique ($arr, $reserveKey = False)
{
if (Is_array ($arr) &&!empty ($arr))
{
foreach ($arr as $key => $value)
{
$TMPARR [$key] = serialize ($value). '';
}
$TMPARR = Array_unique ($TMPARR);
$arr = Array ();
foreach ($tmpArr as $key => $value)
{
if ($reserveKey)
{
$arr [$key] = Unserialize ($value);
}
Else
{
$arr [] = Unserialize ($value);
}
}
}
return $arr;
}
The code is very simple, but very practical, there is a need for small partners can be used directly.
Other users to attach a solution
Copy Code code as follows:
<?php
$input = Array ("A" => "green", "" "," Red "," B "=>" green "," "", "Blue", "Red", "C" => "Witer", "Hello", "witer");
$result = Array_unique ($input); Remove duplicate elements
$result = A_array_unique ($input); Leaving only a single element
foreach ($result as $AA)
{
echo $aa. " <br/> ";
}
function Multi_unique ($array) {
foreach ($array as $k => $na)
$new [$k] = serialize ($NA);
$uniq = Array_unique ($new);
foreach ($uniq as $k => $ser)
$new 1[$k] = unserialize ($ser);
Return ($new 1);
}
function A_array_unique ($array)//write Better
{
$out = Array ();
foreach ($array as $key => $value) {
if (!in_array ($value, $out))
{
$out [$key] = $value;
}
}
return $out;
}
?>
PHP array Remove Duplicates there is a built-in function array_unique (), but the PHP Array_unique function only applies to one-dimensional arrays, which do not apply to multidimensional arrays, the following provides a two-dimensional array of array_unique functions
Copy Code code as follows:
Function Unique_arr ($array 2d, $stkeep =false, $ndformat =true)
{
Determines whether to keep one-level array keys (one-level array keys can be non-numeric)
if ($stkeep) $STARR = Array_keys ($array 2D);
Determines whether to keep level two array keys (All level two array keys must be the same)
if ($ndformat) $NDARR = Array_keys (end ($array 2D));
dimensionality reduction, you can also use implode to convert a one-dimensional array into a comma-connected string
foreach ($array 2D as $v) {
$v = Join (",", $v);
$temp [] = $v;
}
Remove the duplicate string, which is a repeating one-dimensional array
$temp = Array_unique ($temp);
Re-assemble the disassembled array
foreach ($temp as $k => $v)
{
if ($stkeep) $k = $STARR [$k];
if ($ndformat)
{
$TEMPARR = Explode (",", $v);
foreach ($tempArr as $ndkey => $ndval) $output [$k] [$NDARR [$ndkey]] = $ndval;
}
else $output [$k] = Explode (",", $v);
}
return $output;
}
Demonstrate:
$array 2D = Array (' title ' => ' 1111 ', ' Date ' => ' 2222 '), ' second ' =>array (' title ' => ') 1111 ', ' Date ' => ' 2222 '), ' third ' =>array (' title ' => ' 2222 ', ' Date ' => ' 3333 '));
Print_r ($array 2D);
Print_r (Unique_arr ($array 2d,true));