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

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

青灯夜游
Release: 2023-04-04 19:58:02
Original
5491 people have browsed it

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)
Copy after login

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); 
?>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template