<? 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); // only a single element is left.
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)
$ New1 [$ K] = unserialize ($ Ser );
Return ($ new1 );
}
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 deduplication has a built-in function array_unique (), but the PHP array_unique function is only applicable to one-dimensional arrays and not to multidimensional arrays. The following provides a two-dimensional array array_unique Function
Function unique_arr ($ array2d, $ stkeep = false, $ ndformat = true)
{
// Determine whether the primary array key is retained (the primary array key can be non-numeric)
If ($ stkeep) $ Starr = array_keys ($ array2d );
// Determine whether the secondary array key is retained (all secondary array keys must be the same)
If ($ ndformat) $ ndarr = array_keys (end ($ array2d ));
// Dimensionality reduction. You can also use implode to convert a one-dimensional array to a string connected with commas (,).
Foreach ($ array2d as $ v ){
$ V = join (",", $ V );
$ Temp [] = $ V;
}
// Remove the duplicate string, that is, the duplicate one-dimensional array.
$ Temp = array_unique ($ temp );
// Re-assemble the split 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;
}
Demo:
$ Array2d = array ('first' => array ('title' => '123', 'date' => '123 '), 'second' => array ('title' => '123', 'date' => '123 '), 'third' => array ('title' => '123456', 'date' => '123456 '));
Print_r ($ array2d );
Print_r (unique_arr ($ array2d, true ));