php Removing Duplicate Data from Arrays

  • 2021-08-05 09:15:00
  • OfStack

Don't talk nonsense, just go to the code


/**
         * Rearrange an array
         * And array_unique Function: It requires val Is a string, and this can be an array / Object
         *
         * @param unknown_type $arr Array to be reloaded
         * @param unknown_type $reserveKey Do you want to 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 it is very practical. Small partners who need it can use it directly.

Also attached is the solution of other netizens


<?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);   // Leave only a single 1 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)
       $new1[$k] = unserialize($ser);
   return ($new1);
} function a_array_unique($array)// It's better written
{
   $out = array();
   foreach ($array as $key=>$value) {
       if (!in_array($value, $out))
{
           $out[$key] = $value;
       }
   }
   return $out;
}
?>

The PHP array has a built-in function array_unique (), but the array_unique function of php is only applicable to 1-dimensional arrays and not to multidimensional arrays. Here is a array_unique function for 2-dimensional arrays


function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
    // Determine whether to keep it 1 Series array key (1 Series array keys can be non-numeric )
    if($stkeep) $stArr = array_keys($array2D);
    // Determine whether to keep it 2 Series array key ( All 2 Series array keys must be the same )
    if($ndformat) $ndArr = array_keys(end($array2D));
    // Dimension reduction , You can also use implode, Will 1 Dimensional array is converted to a string concatenated with commas
    foreach ($array2D as $v){
        $v = join(",",$v);
        $temp[] = $v;
    }
    // Remove duplicate strings , That is, repeated 1 Dimensional array
    $temp = array_unique($temp);
    // Then reassemble 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;
}

Demo:

$array2D = array('first'= > array('title'= > '1111','date'= > '2222'),'second'= > array('title'= > '1111','date'= > '2222'),'third'= > array('title'= > '2222','date'= > '3333'));
print_r($array2D);
print_r(unique_arr($array2D,true));


Related articles: