Home >Backend Development >PHP Problem >How to check if array contains the same elements in php
PHP is a strongly typed, dynamic language that is very suitable for processing array type data. In actual development, we often need to check whether an array contains the same elements. This requirement may involve many aspects, such as data deduplication, data filtering, etc. Below, this article explains how to check if an array contains the same elements using some common methods in PHP.
1. Use the array_unique function to remove duplicate elements from an array
In PHP, you can use the array_unique function to remove duplicate elements from an array. This function can accept an array as a parameter and returns a new array that does not contain duplicate elements.
The following is a specific sample code:
$originArray = array(1, 2, 3, 2, 4, 5, 4); $newArray = array_unique($originArray); print_r($newArray);
After executing the above code, you can get the output result:
Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 4 [5] => 5 )
As you can see from the above results, in the original array The duplicate elements are successfully deduplicated, and the new array after deduplication is output.
2. Use the array_count_values function to count the number of occurrences of elements
In PHP, you can also use the array_count_values function to count the number of occurrences of each element in the array. This function can accept an array as a parameter and return a new associative array, where each element is a different value in the original array, and the key of the associative array is the number of times the value appears in the original array.
A specific sample code is given below:
$originArray = array(1, 2, 3, 2, 4, 5, 4); $newArray = array_count_values($originArray); print_r($newArray);
After executing the above code, you can get the output result:
Array ( [1] => 1 [2] => 2 [3] => 1 [4] => 2 [5] => 1 )
As you can see from the above results, the original array The number of occurrences of each element in is counted, and an associative array is output. In this associative array, the keys are the values in the original array, and the values are the number of times that element occurs in the original array.
3. Use the array_diff function to compare arrays
In PHP, you can also use the array_diff function to compare whether two arrays have the same elements. This function can accept two arrays as parameters and return a new array containing the different values in the two arrays.
A specific sample code is given below:
$originArray1 = array(1, 2, 3, 2, 4, 5, 4); $originArray2 = array(6, 7, 8, 9, 10); $diffArray = array_diff($originArray1, $originArray2); print_r($diffArray);
After executing the above code, you can get the output result:
Array ( [0] => 1 [2] => 3 [4] => 5 )
As you can see from the above results, two The same elements exist in the arrays, and in the new array only those elements that appear in the first array but not in the second array are output.
4. Use the array_intersect function to compare arrays
Similar to the array_diff function, PHP also provides the array_intersect function to compare whether there are the same elements between two arrays. This function accepts two arrays as parameters and returns a new array containing the intersection of the two arrays.
The following is a specific sample code:
$originArray1 = array(1, 2, 3, 2, 4, 5, 4); $originArray2 = array(2, 4, 5, 6, 7); $intersectArray = array_intersect($originArray1, $originArray2); print_r($intersectArray);
After executing the above code, you can get the output result:
Array ( [1] => 2 [4] => 4 [5] => 5 )
As you can see from the above results, two arrays The same elements exist in both arrays, and only those elements that appear in both arrays are output in the new array.
The above are several common PHP functions used to check whether arrays contain the same elements, including array deduplication, counting the number of occurrences of elements, comparing arrays, etc. Of course, other PHP functions or custom functions can also be used to achieve the same purpose in actual applications.
The above is the detailed content of How to check if array contains the same elements in php. For more information, please follow other related articles on the PHP Chinese website!