Home  >  Article  >  Backend Development  >  php去除数组中重复数据_php实例

php去除数组中重复数据_php实例

WBOY
WBOYOriginal
2016-06-07 17:15:32775browse

废话不多说,直接上代码

复制代码 代码如下:

/**
         * 给数组排重
         * 与array_unique函数的区别:它要求val是字符串,而这个可以是数组/对象
         *
         * @param unknown_type $arr 要排重的数组
         * @param unknown_type $reserveKey 是否保留原来的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;
        }

代码很简单,却非常的实用,有需要的小伙伴可以直接拿去用的。

另附上其他网友的解决办法

复制代码 代码如下:

$input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer");
//$result = array_unique($input); //去除重复元素
$result = a_array_unique($input);   //只留下单一元素
foreach($result as $aa)
{
echo $aa."
";
}
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)//写的比较好
{
   $out = array();
   foreach ($array as $key=>$value) {
       if (!in_array($value, $out))
{
           $out[$key] = $value;
       }
   }
   return $out;
}
?>

PHP数组去除重复项 有个内置函数array_unique (),但是php的 array_unique函数只适用于一维数组,对多维数组并不适用,以下提供一个二维数组 的 array_unique函数

复制代码 代码如下:

function unique_arr($array2D,$stkeep=false,$ndformat=true)
{
    // 判断是否保留一级数组键 (一级数组键可以为非数字)
    if($stkeep) $stArr = array_keys($array2D);
    // 判断是否保留二级数组键 (所有二级数组键必须相同)
    if($ndformat) $ndArr = array_keys(end($array2D));
    //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
    foreach ($array2D as $v){
        $v = join(",",$v);
        $temp[] = $v;
    }
    //去掉重复的字符串,也就是重复的一维数组
    $temp = array_unique($temp);
    //再将拆开的数组重新组装
    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;
}

演示:

$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));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn