Comparison steps: 1. Use the array_intersect() function to compare multiple arrays and obtain the repeated values (intersection elements) of the array. The syntax "array_intersect(array 1, array 2, array 3...)" will Return an intersection array; 2. Determine whether the intersection array is empty. The syntax is "intersection array ===[]". If it is empty, there are no duplicate values in multiple arrays. If it is not empty, there are duplicate values in multiple arrays.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
Compare multiple arrays and determine whether multiple arrays are If there are duplicate values, it means to determine whether multiple arrays have intersection elements.
Judgment steps:
Step 1: Use the array_intersect() function to compare multiple arrays and obtain the repeated values (intersection elements) of the arrays
array_intersect() function is used to compare the values of two (or more) arrays and return the intersection.
This function compares the values of two (or more) arrays and returns an intersection array containing all values in array1 that also appear in all other parameter arrays.
<?php header('content-type:text/html;charset=utf-8'); $arr1=array(0,1,2,3,4,5,6,7,8,9); $arr2=array(0,2,4,6,8,10,12,14,16); $arr3=array(0,2,4,8,16,32); var_dump($arr1); var_dump($arr2); var_dump($arr3); echo "多个数组的重复元素:"; $intersect=array_intersect($arr1,$arr2,$arr3); var_dump($intersect); ?>
Step 2: Determine whether the intersection array is empty
交集数组===[]
If it is empty, then more There are no duplicate values in arrays
If they are not empty, there are duplicate values in multiple arrays
Complete example implementation code :
<?php header('content-type:text/html;charset=utf-8'); $arr1=array(0,1,2,3,4,5,6,7,8,9); $arr2=array(0,2,4,6,8,10,12,14,16); $arr3=array(0,2,4,8,16,32); var_dump($arr1); var_dump($arr2); var_dump($arr3); $intersect=array_intersect($arr1,$arr2,$arr3); if($intersect===[]){ echo "多个数组中没有重复元素"; }else{ echo "多个数组中有重复元素"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to compare multiple arrays in php to see if there are duplicate values. For more information, please follow other related articles on the PHP Chinese website!