In PHP, you can use the built-in function array_intersect() to find the intersection of two arrays, which can return the common elements (intersection part) of the two arrays. Let’s introduce it in detail below.
##array_intersect() function
Basic syntax:array_intersect($ array1,$ array2)
Simple example
Let’s take a look at the array_intersect() function’s method of finding the intersection of two arrays through an example
<?php // 定义两个数组 $array1 = array(2, 5, 7, 6, 9); $array2 = array(3, 2, 5, 6, 8); // 找到两个数组的交集 $result = array_intersect($array1, $array2); // 重新索引 $result = array_values($result); // 输出结果数组(交集) var_dump($result); ?>
The above is the detailed content of How to find the intersection of two arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!