Home  >  Article  >  php教程  >  php去除二维数组中重复值程序

php去除二维数组中重复值程序

WBOY
WBOYOriginal
2016-06-08 17:24:171270browse

具体的思想就是把二维数组转化为一位数组,然后用array_unique()去除一维数组中的重复值,最后再将一维数组转化为二维数组!

例1

 代码如下 复制代码

function my_array_unique($array2D){ 
    
    foreach ($array2D as $v){
         $v = implode(“,”,$v);             
         $temp[] = $v;
     }
    $temp = array_unique($temp);     
    foreach ($temp as $k => $v){
        $temp[$k] = explode(“,”,$v);
    }
    return $temp;
}

下面推荐一种非常不错的去除二维数组重复值函数

 代码如下 复制代码

function a_array_unique($array)//写的比较好
{
$out = array();
foreach ($array as $key=>$value) {
if (!in_array($value, $out))
{
$out[$key] = $value;
}
}
return $out;
}

使用方法也是当然简单的如

 代码如下 复制代码

$array2D = array(‘first’=>array(‘title’=>’1111′,’date’=>’2222′),’second’=>array(‘title’=>’1111′,’date’=>’2222′),’third’=>array(‘title’=>’2222′,’date’=>’3333′));

调用方法

 代码如下 复制代码

$as = a_array_unique($array2D);
print_r($as);

即可了。

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