Home > Backend Development > PHP Tutorial > 两种php去除二维数组的重复项方法_PHP

两种php去除二维数组的重复项方法_PHP

WBOY
Release: 2016-05-29 11:48:02
Original
904 people have browsed it

php去掉二维数组的重复值的方法总结,具体代码如下:
方法一:

//二维数组去掉重复值
function array_unique_fb($array2D){
 foreach ($array2D as $v){
  $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
  $temp[]=$v;
 }
 $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
 foreach ($temp as $k => $v){
  $temp[$k]=explode(',',$v); //再将拆开的数组重新组装
 }
 return $temp;
}
Copy after login

方法二:

//二维数组去掉重复值,并保留键值
function array_unique_fb($array2D){
 foreach ($array2D as $k=>$v){
  $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
  $temp[$k]=$v;
 }
 $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组 
 foreach ($temp as $k => $v){
  $array=explode(',',$v); //再将拆开的数组重新组装
  //下面的索引根据自己的情况进行修改即可
  $temp2[$k]['id'] =$array[0];
  $temp2[$k]['title'] =$array[1];
  $temp2[$k]['keywords'] =$array[2];
  $temp2[$k]['content'] =$array[3];
 }
 return $temp2;
}


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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template