php去除二维数组的重复项方法_php技巧

WBOY
Release: 2016-05-16 20:05:33
Original
933 people have browsed it

php中去一维数组的重复项可以通过php内置函数array_unique(),但是php的array_unique函数对多维数组并不适用,怎么才能去除二维数组中的重复项呢?

以下给大家提供一个函数。

//二维数组去掉重复值 function unique_arr($array2D,$stkeep=false,$ndformat=true){ $joinstr='+++++'; // 判断是否保留一级数组键 (一级数组键可以为非数字) if($stkeep) $stArr = array_keys($array2D); // 判断是否保留二级数组键 (所有二级数组键必须相同) if($ndformat) $ndArr = array_keys(end($array2D)); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串 foreach ($array2D as $v){ $v = join($joinstr,$v); $temp[] = $v; } //去掉重复的字符串,也就是重复的一维数组 $temp = array_unique($temp); //再将拆开的数组重新组装 foreach ($temp as $k => $v){ if($stkeep) $k = $stArr[$k]; if($ndformat){ $tempArr = explode($joinstr,$v); foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval; } else $output[$k] = explode($joinstr,$v); } return $output; }
Copy after login

希望对大家学习php程序设计有所帮助。

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
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!