Home >Backend Development >PHP Problem >Find the same value in php arrays
As a popular open source programming language, PHP can efficiently handle a series of problems, one of which is processing arrays. When processing arrays, PHP also provides many powerful functions. In this article, we will mainly discuss how to find the same elements in an array.
Methods to find the same elements
In PHP, there are different methods to find the same elements in an array. Next we will introduce some of the basic methods:
Method 1: Use array_count_values function
array_count_values function can easily find the number of occurrences of each element in the array, and then we only need to filter the elements whose occurrence number is greater than 1. The sample code is as follows:
$arr = array(1,2,3,4,5,3,2,1,2,5,5); $counts = array_count_values($arr); foreach($counts as $key=>$value){ if($value > 1){ echo "元素".$key."出现了".$value."次"."<br/>"; } }
Method 2: Use array_unique and array_diff functions
The array_unique function can return the array after removing duplicate elements, while the array_diff function can return the difference between the two arrays. Through the combination of these two functions, we can find the same elements in the original array. The sample code is as follows:
$arr = array(1,2,3,4,5,3,2,1,2,5,5); $unique_arr = array_unique($arr); $diff_arr = array_diff_assoc($arr,$unique_arr); foreach($diff_arr as $value){ echo "元素".$value."是相同的"."<br/>"; }
Method 3: Use the array_intersect function
The array_intersect function returns the intersection part of the two arrays, and then uses the array_unique function to remove duplicates to obtain the same elements. The sample code is as follows:
$arr1 = array(1,2,3,4,5); $arr2 = array(3,4,5,6,7); $intersect_arr = array_intersect($arr1,$arr2); $unique_arr = array_unique($intersect_arr); foreach($unique_arr as $value){ echo "元素".$value."是相同的"."<br/>"; }
Method 4: Traverse the array
The simplest and most primitive method is to directly traverse the array through a loop to find the same elements. The sample code is as follows:
$arr = array(1,2,3,4,5,3,2,1,2,5,5); for($i=0;$i<count($arr);$i++){ for($j=$i+1;$j<count($arr);$j++){ if($arr[$i] == $arr[$j]){ echo "元素".$arr[$i]."和元素".$arr[$j]."是相同的"."<br/>"; } } }
The above are four common methods. In fact, there are many methods of array operation, and there are many efficient and concise methods that can be used.
Ideas and precautions
For the problem of finding the same elements, we need certain thinking and precautions:
Summary
Processing the same elements in an array is a very common operation in PHP. We can solve this problem in several ways. When choosing a solution method, the optimal method should be selected based on actual needs and array size. In addition, there are many techniques and methods for operating arrays, which need to be continuously practiced and summarized in actual programming.
The above is the detailed content of Find the same value in php arrays. For more information, please follow other related articles on the PHP Chinese website!