PHP二维数组去重复项自定义函数
如果我们是一维数据去重复项就不要说了有直接的函数了,但二维数据php未提供函数了,下面小编来给大家推荐两个不错的二维数组去重复项自定义函数吧.
例1代码如下:
- function unique_array_2d($array2D,$stkeep=false,$ndformat=true)
- {
-
- if($stkeep) $stArr = array_keys($array2D);
-
-
- if($ndformat) $ndArr = array_keys(end($array2D));
-
-
- foreach ($array2D as $v){
- $v = join(",",$v);
- $temp[] = $v;
- }
-
-
- $temp = array_unique($temp);
-
-
- foreach ($temp as $k => $v)
- {
- if($stkeep) $k = $stArr[$k];
- if($ndformat)
- {
- $tempArr = explode(",",$v);
- foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
- }
- else $output[$k] = explode(",",$v);
- }
-
- return $output;
- }
-
- $array2D = array(
- 'first'=>array('title'=>'1111','date'=>'2222'),
- 'second'=>array('title'=>'1111','date'=>'2222'),
- 'third'=>array('title'=>'2222','date'=>'3333')
- );
-
- echo "<pre>";
- print_r(unique_array_2d($array2D,true));
-
-
-
-
-
-
-
-
- function array_multunique($arr,$by = array()) {
- $temp = array();
- foreach($arr as $key => $val) {
- foreach($by as $v) {
- $temp[$key] .= isset($val[$v]) ? $val[$v] : '';
- }
- }
- return array_intersect_key($arr,array_unique($temp));
- }
-
-
-
-
-
-
-
-
-
-
- function array_remove_key($array,$keys){
- if (!is_array($array) || !is_array($keys)){
- return false;
- }
- foreach($array as $t){
- foreach($keys as $k){
- unset($t[$k]);
- }
- $doc[]=$t;
- }
- return $doc;
-
- }
-
-
-
-
-
-
- function array_remove_key_val(&$a,$b,$c){
- foreach ($a as $key=>$value){
- if ( isset($value[$b]) && ($value[$b]==$c) ){
- unset($a[$key]);
- }
- }
- }
-
-
-
-
-
例2代码如下:
- /二维数组去掉重复值
- function array_unique_fb($array2D){
- foreach ($array2D as $v){
- $v = join(",",$v);
- $temp[] = $v;
- }
- $temp = array_unique($temp);
- foreach ($temp as $k => $v){
- $temp[$k] = explode(",",$v);
- }
- return $temp;
- }
-
-
-
- function array_unique_fb($array2D){
- foreach ($array2D as $k=>$v){
- $v = join(",",$v);
- $temp[$k] = $v;
- }
- $temp = array_unique($temp);
- foreach ($temp as $k => $v){
- $array=explode(",",$v);
- $temp2[$k]["id"] =$array[0];
- $temp2[$k]["litpic"] =$array[1];
- $temp2[$k]["title"] =$array[2];
- $temp2[$k]["address"] =$array[3];
- $temp2[$k]["starttime"] =$array[4];
- $temp2[$k]["endtime"] =$array[5];
- $temp2[$k]["classid"] =$array[6];
- $temp2[$k]["ename"] =$array[7];
- }
- return $temp2;
- }