Home > Article > Backend Development > How to remove duplicate data in php
php method to remove duplicate data: 1. Remove duplicate data from a one-dimensional array through the array_unique function; 2. Remove duplicate data from a multi-dimensional array through the custom unique_arr method.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
php How to remove duplicate data?
PHP removes duplicate array data
<?php $input = array("a" => "green","", "red","b" => "green", "","blue", "red","c" => "witer","hello","witer"); //$result = array_unique($input); //去除重复元素 $result = a_array_unique($input); //只留下单一元素 foreach($result as $aa) { echo $aa."<br />"; } function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); } function a_array_unique($array)//写的比较好 { $out = array(); foreach ($array as $key=>$value) { if (!in_array($value, $out)) { $out[$key] = $value; } } return $out; } ?>
PHP array has a built-in function array_unique () to remove duplicates, but php The array_unique function is only applicable to one-dimensional arrays, not multi-dimensional arrays. The following provides an array_unique function for a two-dimensional array
function unique_arr($array2D,$stkeep=false,$ndformat=true) { // 判断是否保留一级数组键 (一级数组键可以为非数字) if($stkeep) $stArr = array_keys($array2D); // 判断是否保留二级数组键 (所有二级数组键必须相同) if($ndformat) $ndArr = array_keys(end($array2D)); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串 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; }
Demonstration:
$array2D = array('first'=>array('title'=>'1111','date'=>'2222'),'second'=>array('title'=>'1111','date'=>'2222'),'third'=>array('title'=>'2222','date'=>'3333')); print_r($array2D); print_r(unique_arr($array2D,true));
Method 2:
Example 1
1, PHP one-dimensional array to remove duplicates (use array_unique function).
Example 1, code:
<?php $aa=array("apple","banana","pear","apple","wail","watermalon"); $bb=array_unique($aa); print_r($bb); ?>
Output result:
Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon )
Second, duplicate items of PHP two-dimensional array:
For two-dimensional arrays, we divide Two situations are discussed, one is to delete duplicates because the value of a certain key name cannot be repeated;
The other is to delete duplicates because the internal one-dimensional array cannot be exactly the same.
Example 1, because the value of a certain key name cannot be repeated, delete the duplicates.
Code:
<?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 { // www.jbxue.com 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); ?>
Output result:
Array ( [0] => Array ( [id] => 123 [name] => 张三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 赵六 ) [3] => Array ( [id] => 126 [name] => 赵六 ) )
Example 2, because the internal one-dimensional array cannot be exactly the same, duplicate items are deleted.
Code:
<?php function array_unique_fb($array2D){ foreach ($array2D as $v){ $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串 $temp[] = $v; } // www.jbxue.com $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) ?> Array ( [0] => Array ( [0] => 123 [1] => 张三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] => 126 [1] => 赵六 ) )
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to remove duplicate data in php. For more information, please follow other related articles on the PHP Chinese website!