Method: 1. Use array_intersect() to compare the values of two arrays. The syntax "array_intersect(array 1, array 2)" will return an intersection array; 2. Determine whether the intersection array is empty. The syntax " Intersection array == []", if it is empty, it does not have the same value, if it is not empty, it has the same value.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php determines two Whether the arrays have the same value
1. Use the array_intersect() function to compare two arrays and obtain the intersection elements
array_intersect() function is used Compares the values of two (or more) arrays and returns an intersection array.
If the two arrays have the same value, then there are elements in the intersection array, not an empty array
If the two arrays do not have the same value, Then there are no elements in the intersection array and it is an empty array
<?php header("Content-type:text/html;charset=utf-8"); $arr1=array(1,2,3,4,5); $arr2=array(2,4,6,8,10); $arr3=array(1,3,5,7,9); echo "数组1和数组2的交集:<br>"; $intersect=array_intersect($arr1,$arr2); var_dump($intersect); echo "数组2和数组3的交集:<br>"; $intersect=array_intersect($arr3,$arr2); var_dump($intersect); ?>
2. Determine whether the intersection array is an empty array
If it is an empty array, the two arrays do not have the same value
If it is not an empty array, the two arrays have the same value
echo "数组1和数组2的交集:<br>"; $intersect=array_intersect($arr1,$arr2); var_dump($intersect); if($intersect==[]){ echo "两个数组没有相同值<br><br><br>"; }else{ echo "两个数组有相同值<br><br><br>"; } echo "数组2和数组3的交集:<br>"; $intersect=array_intersect($arr3,$arr2); var_dump($intersect); if($intersect==[]){ echo "两个数组没有相同值<br>"; }else{ echo "两个数组有相同值<br>"; }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine if two arrays have the same value in php. For more information, please follow other related articles on the PHP Chinese website!