Home >php教程 >php手册 >php三维数组去重(示例代码)

php三维数组去重(示例代码)

WBOY
WBOYOriginal
2016-06-13 09:30:501464browse

假设叫数组 $my_array;

复制代码 代码如下:


// 新建一个空的数组.
$tmp_array = array();

$new_array = array();

// 1. 循环出所有的行. ( $val 就是某个行)
foreach($my_array as $k => $val){

    $hash = md5(json_encode($val));
    if (in_array($hash, $tmp_array)) {
        echo('这个行已经有过了');
    }else{
        // 2. 在 foreach 循环的主体中, 把每行数组对象得hash 都赋值到那个临时数组中.
        $tmp_array[] = $hash;
        $new_array[] = $val;
    }
}

print_r($new_array);


$new_array 即为筛选后无重复数据的数组。
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