php数组去重方法

技术博客 (290) 2024-01-01 18:01:01

一维数组的重复项

<?php
$aa = array("apple", "banana", "pear", "apple", "wail", "watermalon");
$bb = array_unique($aa);
print_r($bb);

 

二维数组去重项

$result = array(
    0=>array('a'=>1,'b'=>'Hello'),
    1=>array('a'=>1,'b'=>'other'),
    2=>array('a'=>1,'b'=>'other'),
);

处理成

$result = array(
    0=>array('a'=>1,'b'=>'Hello'),
    1=>array('a'=>1,'b'=>'other')   
);

使用方法

array_unique($result, SORT_REGULAR);

 

PHP多维数组去重

/**
 * @param  array  $arr [description]
 * @return [type]      [description]
 */
static function super_unique($array, $recursion = false){

   // 序列化数组元素,去除重复
   $result = array_map('unserialize', array_unique(array_map('serialize', $array)));
   // 递归调用
   if ($recursion) {

      foreach ($result as $key => $value) {

         if (is_array($value)) {

            $result[ $key ] = super_unique($value);
         }
      }
   }
   return $result;
}

 

PHP实现的多维数组去重操作示例

<?php
$str = '{

  "status": 0,
  "result": [
    {

      "hostName": "杨新",
      "pcode2": "41675441",
      "email": "xin.yang@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "杨新",
      "pcode2": "41675441",
      "email": "xin.yang@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "中信国安科技控股有限公司",
      "pcode2": "9730284461",
      "email": "liuxiao@gakj.xx.com",
      "selfName": "邵兵"
    },
    {

      "hostName": "中信国安科技控股有限公司",
      "pcode2": "9730284461",
      "email": "liuxiao@gakj.xx.com",
      "selfName": "邵兵"
    },
    {

      "hostName": "刘惠",
      "pcode2": "64068956",
      "email": "huijie.liu@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "刘惠",
      "pcode2": "64068956",
      "email": "huijie.liu@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "刘凯南",
      "pcode2": "14818360",
      "email": "kainan.liu_3@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "刘凯南",
      "pcode2": "14818360",
      "email": "kainan.liu_3@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "宋纬奇",
      "pcode2": "62947142",
      "email": "weiqi.song@xx.cn",
      "selfName": "邵兵"
    },
    {

      "hostName": "宋纬奇",
      "pcode2": "62947142",
      "email": "weiqi.song@xx.cn",
      "selfName": "邵兵"
    }
  ],
  "content_type": "application/json"
}';
$res = json_decode($str,true);
echo 'num: '.count($res['result']);
print_r($res['result']);
echo "</br></br></br>";
$rrr = $res['result'];
$serializeArrs = array_map('serialize',$rrr);
$uniqueArrs = array_unique($serializeArrs);
$unserializeArrs = array_map('unserialize',$uniqueArrs);
print_r($unserializeArrs);
echo "</br></br></br>";
echo json_encode($unserializeArrs);
exit;

 

因为某一键名的值不能重复,删除重复项

<?php
function assoc_unique($arr, $key) {

    $tmp_arr = array();
    foreach ($arr as $k => $v) {

        if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
            unset($arr[$k]);
        } else {

            $tmp_arr[] = $v[$key];
        }
    }
    sort($arr); //sort函数对数组进行排序
    return $arr;
}$aa = array(
    array('id' => 123, 'name' => '张三'),
    array('id' => 123, 'name' => '李四'),
    array('id' => 124, 'name' => '王五'),
    array('id' => 125, 'name' => '赵六'),
    array('id' => 126, 'name' => '赵六')
);
$key = 'id';
assoc_unique(&$aa, $key);
print_r($aa);

 

PHP多维数组去重

<?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;
}
 
$aa = array(
    array('id' => 123, 'name' => '张三'),
    array('id' => 123, 'name' => '李四'),
    array('id' => 124, 'name' => '王五'),
    array('id' => 123, 'name' => '李四'),
    array('id' => 126, 'name' => '赵六')
);
$bb = array_unique_fb($aa);
print_r($bb)

 

多维数组根据某个特定字段过滤重复值

//假如 数据是这样的 $arr=[ [ 'goods_name'=>'xxx', 'goods_id'=>111, ], [
'goods_name'=>'xxx',
  'goods_id'=>123,
],
[
'goods_name'=>'xxx',
'goods_id'=>'123'
],
];
function array_unset_tt($arr,$key='goods_id'){

$res = array();
    foreach ($arr as $value) {

        //查看有没有重复项
        if(isset($res[$value[$key]])){

            unset($value[$key]);  //有:销毁
        }else{

            $res[$value[$key]] = $value;
        }
    }
    return $res;
}
调用方法处理后,就把重复的goods_id的数组过滤,只剩下一个!!!开心吗?

 

 

 

 

 

THE END

发表回复