Home > php教程 > PHP源码 > body text

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

WBOY
Release: 2016-06-08 17:24:17
Original
1330 people have browsed it

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

<script>ec(2);</script>

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

即可了。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!