Home > Article > Backend Development > How to determine whether array numbers are repeated in PHP and how to remove duplicates
In daily development, we often encounter the need to determine whether array numbers are repeated and to remove them, especially when processing form data, statistical data, etc. This article will introduce several ways to determine whether array numbers are repeated and how to remove them in PHP.
1. Use the count and array_unique functions
In PHP, you can use the count function to count the number of array elements, and the array_unique function can remove duplicate elements in the array. Using these two functions together, you can determine whether the numbers in the array are repeated and remove them.
Sample code:
$arr = array(1,3,2,3,4,1,5);//测试数组 $count = count($arr);//统计数组元素的个数 $unique_count = count(array_unique($arr));//去重后的数组元素个数 if($count == $unique_count){ echo '数组中没有重复的数字'; } else { echo '数组中有重复的数字'; }
The output result is:
数组中有重复的数字
2. Use the in_array function
The in_array function in php is used to determine whether an element is in in the array. You can use the in_array function to traverse the array and determine whether there are duplicate elements in the array.
Sample code:
$arr = array(1,3,2,3,4,1,5);//测试数组 $repeat_flag = false;// 判断数组中是否存在重复元素的标志 for($i=0;$i<count($arr)-1;$i++){ for($j=$i+1;$j<count($arr);$j++){ if($arr[$i] == $arr[$j]) {// 数组中存在重复元素 $repeat_flag = true; break;// 跳出循环 } } } if($repeat_flag){ echo '数组中有重复的数字'; } else { echo '数组中没有重复的数字'; }
The output result is:
数组中有重复的数字
3. Use the array_count_values function
The array_count_values function in php can count the same elements in the array number, and returns an associative array, where the key represents the value of the element in the array and the value represents the number of times the element appears in the array. You can use the array_count_values function to count the number of occurrences of each element in the array. If an element appears more than 1, it means that the element is repeated.
Sample code:
$arr = array(1,3,2,3,4,1,5);//测试数组 $n_arr = array_count_values($arr);// 利用array_count_values函数统计数组中每个元素出现次数 $repeat_flag = false;// 判断数组中是否存在重复元素的标志 foreach($n_arr as $n){ if($n>1) {// 数组中存在重复元素 $repeat_flag=true; break;// 跳出循环 } } if($repeat_flag){ echo '数组中有重复的数字'; } else { echo '数组中没有重复的数字'; }
The output result is:
数组中有重复的数字
4. Use array_flip and array_keys functions
The array_flip function in php can exchange the keys in the array keys and values, and returns a new array. Combined with the array_keys function, the values in the new array can be used as keys in the original array to determine whether there are duplicate elements in the original array.
Sample code:
$arr = array(1,3,2,3,4,1,5);//测试数组 $new_arr = array_flip($arr);// 利用array_flip函数交换数组中的键和值 $repeat_flag=false;// 判断数组中是否存在重复元素的标志 foreach(array_keys($new_arr) as $k){// 将新数组中的值作为原数组中的键 if(count($arr)-count(array_keys($arr,$k))>1){// 判断原数组中是否存在重复元素 $repeat_flag=true;// 存在重复元素 break; } } if($repeat_flag){ echo '数组中有重复的数字'; } else { echo '数组中没有重复的数字'; }
The output result is:
数组中有重复的数字
5. Use array_unique and array_diff functions to determine whether there are duplicate elements in the array
In php The array_diff function can calculate the difference of an array and return the difference array. Combined with the array_unique function, you can determine the difference between the original array and the deduplicated array, and then determine whether there are duplicate elements in the original array.
Sample code:
$arr = array(1,3,2,3,4,1,5);//测试数组 $unique_arr = array_unique($arr);// 利用array_unique函数去重 $diff_arr = array_diff($arr,$unique_arr);// 计算原数组与去重后的数组的差集,并返回差集数组 if(!empty($diff_arr)){// 差集不为空,说明原数组中存在重复元素 echo '数组中有重复的数字'; } else { echo '数组中没有重复的数字'; }
The output result is:
数组中有重复的数字
The above are several implementation methods for determining whether array numbers are repeated and deduplicating them in PHP. Different methods have their own advantages and disadvantages. We can choose the appropriate method according to the actual situation to achieve better results.
The above is the detailed content of How to determine whether array numbers are repeated in PHP and how to remove duplicates. For more information, please follow other related articles on the PHP Chinese website!