Home  >  Article  >  Backend Development  >  PHP uses array_intersect() function to find the intersection of two arrays

PHP uses array_intersect() function to find the intersection of two arrays

青灯夜游
青灯夜游Original
2019-01-19 17:58:055408browse

The array_intersect() function is a built-in function provided by PHP that returns the common (intersected) elements of two arrays. The following article will introduce you to the array_intersect() function and introduce how to use the array_intersect() function to find the intersection of two arrays. I hope it will be helpful to you.

PHP uses array_intersect() function to find the intersection of two arrays

php array_intersect() function

array_intersect() function is used to compare two (or more Key values ​​of multiple) arrays and return the intersection. [Recommended related video tutorials: PHP Tutorial]

Basic sentence structure:

array_intersect($ array1,$ array2)

Return value: Return an array , which contains all values ​​of array1 present in array2.

Note: Since the array_intersect() function returns an array with reserved keys, we also need to use array_values() to reorder the keys.

Code Example

Let’s use a simple example to introduce the method of array_intersect() function array_values() to find the intersection of two arrays

<?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); 
?>

Output:

PHP uses array_intersect() function to find the intersection of two arrays

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of PHP uses array_intersect() function to find the intersection of two arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn