php去除数组中所有相似的值

最近处理表格文件中,遇到需要按数组某一个重复值去重,才有此志~~~~~~

//二维数组中将某一个字段(mobile)相同值去掉
$tmpData=[ 
		[
			"enterprise_id"=> 3 ,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110000,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 4 ,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110000,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 5 ,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110001,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 6 ,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110001,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 7	,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110004,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 8	,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110004,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 9	,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110004,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
		[
			"enterprise_id"=> 10	,
			"name"=> "张三", 
			"sex"=> 1, 
			"birth_date"=> "1990-08-09" ,
			"mobile"=> 13011110005,
			"email"=> "[email protected]" ,
			"department_id"=> 49, 
			"position_name"=> "前台" ,
			"entry_date"=>  "1990-08-09" ,
			"created_at"=> 1533820975,
			"updated_at"=> 1533820975
		],
	];
	
	//取出所有key值对应的电话号码
	foreach ($tmpData as $key => $value) {
		$mobileData[$key]=$value['mobile'];
	}

	//取唯一值,为所有重复值添加状态status
	$unique = array_unique($mobileData);
	$diff=array_diff_assoc($mobileData, $unique);

	foreach ($tmpData as $key => &$val) {
		foreach ($diff as $k => $v) {
			if($v==$val['mobile'])
			{
				$val['status'] =1;
			}
		}
	}

	//此步可不加,将手机号码重复数据去不重复分开
	$data=[];
	foreach ($tmpData as $key => $value) {
		if(isset($value['status'])){
			unset($value['status']);
			$value['errors']='重复';
			$data['failed'][$key]=$value;
		}else{
			$data['success'][$key]=$value;
		}
	}
	print_r($data);

OK,看效果,是不是你需要的

Array
(
    [failed] => Array
        (
            [0] => Array
                (
                    [enterprise_id] => 3
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110000
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [1] => Array
                (
                    [enterprise_id] => 4
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110000
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [2] => Array
                (
                    [enterprise_id] => 5
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110001
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [3] => Array
                (
                    [enterprise_id] => 6
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110001
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [4] => Array
                (
                    [enterprise_id] => 7
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110004
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [5] => Array
                (
                    [enterprise_id] => 8
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110004
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

            [6] => Array
                (
                    [enterprise_id] => 9
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110004
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                    [errors] => 重复
                )

        )

    [success] => Array
        (
            [7] => Array
                (
                    [enterprise_id] => 10
                    [name] => 张三
                    [sex] => 1
                    [birth_date] => 1990-08-09
                    [mobile] => 13011110005
                    [email] => [email protected]
                    [department_id] => 49
                    [position_name] => 前台
                    [entry_date] => 1990-08-09
                    [created_at] => 1533820975
                    [updated_at] => 1533820975
                )

        )

)

猜你喜欢

转载自blog.csdn.net/qq_40816112/article/details/81808160