Home >Backend Development >PHP Problem >How to compare two arrays in php
PHP is a widely used server-side scripting language that is widely used in website development and data interaction. Comparing two arrays is also one of the basic operations. This article will introduce several different methods to compare two arrays.
1. Use the == and === operators
Using the "==" and "===" operators is a common way to compare two arrays. The difference between the two operators is that the "==" operator compares whether the key values of the two arrays are the same, while the "===" operator also compares whether the types of the two arrays are the same.
For example, assuming there are two arrays, $arr1 = array(1, 2, 3); and $arr2 = array(1, 2, 3);, use the "==" operator to compare the two arrays. Arrays:
if($arr1 == $arr2){
echo "两个数组相同";
}else{
echo "两个数组不同";
}
If you use "===" The operator compares two arrays:
if($arr1 === $arr2){
echo "两个数组相同";
}else{
echo "两个数组不同";
}
2. Using the array_diff function
Using the array_diff function is also a way to compare two arrays. This function returns a new array consisting of the elements in array1 that are not in array2.
For example, assuming there are two arrays, $arr1 = array(1, 2, 3); and $arr2 = array(2, 3);, use the array_diff function to compare the two arrays:
$diff = array_diff($arr1, $arr2);
if(count($diff) == 0){
echo "两个数组相同";
}else{
echo "两个数组不同";
}
In the above example, if the length of the $diff array is 0, it means that the elements of $arr1 and $arr2 are the same, and the output is "the two arrays are the same", otherwise it means that the elements of $arr1 and $arr2 are the same. Different, the output is "the two arrays are different".
3. Use the array_intersect function
Using the array_intersect function is also a way to compare two arrays. This function returns a new array consisting of elements contained in both array1 and array2.
For example, assuming there are two arrays, $arr1 = array(1, 2, 3); and $arr2 = array(2, 3);, use the array_intersect function to compare the two arrays:
$intersect = array_intersect($arr1, $arr2);
if(count($intersect) == count($arr1)){
echo "两个数组相同";
}else{
echo "两个数组不同";
}
In the above example, if the number of elements in the $intersect array is equal to the number of elements in $arr1, it means that the elements of $arr1 and $arr2 are the same, and output "the two arrays are the same", otherwise It means that the elements of $arr1 and $arr2 are different, and the output is "the two arrays are different".
4. Use the array_udiff function
Using the array_udiff function is also a way to compare two arrays. This function compares the elements in array 1 with the elements in array 2 and if the two elements are considered equal, they are not included in the resulting array.
For example, assuming there are two arrays, $arr1 = array(1, 2, 3); and $arr2 = array(2, 3);, use the array_udiff function to compare the two arrays:
$diff = array_udiff($arr1, $arr2, function($a, $b){
if($a === $b){ return 0; } return ($a > $b) ? 1 : -1;
});
If the length of the $diff array is 0, it means If the elements of $arr1 and $arr2 are the same, output "the two arrays are the same". Otherwise, it means that the elements of $arr1 and $arr2 are different, and output "the two arrays are different".
Summary
The above introduces four methods of comparing two arrays, each method has its advantages and disadvantages. By using different methods, we can choose the most suitable method according to the specific application scenario. At the same time, it should be noted that when comparing two arrays, especially when using the "==" operator, pay attention to the order of the elements in the array, because the different order of elements in the array may also cause the arrays to be different.
The above is the detailed content of How to compare two arrays in php. For more information, please follow other related articles on the PHP Chinese website!