admin管理员组

文章数量:815275

PHP二维数组去重方法大全

1.简单的二维数组去重

function array_unique_fb($array2D=[]) {$temp = [];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;
}

 2.二维数组去掉重复值 保留键名

/*** @param  [type] $array2D [二维数组]* @param  [type] $keys    [全部键名]* @return [type]          [去重后的数组]*/
function array_unique_fb($array2D=[], $keys=[]){$temp = [];$temp2 = [];foreach ($array2D as $k=>$v){  $v=join(',',$v);  $temp[$k]=$v;  }  $temp=array_unique($temp);foreach ($temp as $k => $v){  $array=explode(',',$v);foreach($keys as $index => $key)  {    $temp2[$k][$key] = $array[$index];  }  }  return $temp2;  
}

 3.二位数组按某个键判断是否重复去重

/*** @param  [type] $arr [description]* @param  [type] $key [description]* @return [type]      [description]*/
function array_unset_tt($arr=[],$key=null){   //建立一个目标数组$res = array();      foreach ($arr as $value) {       //查看有没有重复项if(isset($res[$value[$key]])){unset($value[$key]);}else{$res[$value[$key]] = $value;}  }return $res;
}

本文标签: PHP二维数组去重方法大全