The php **array_intersect()** function allows us to find the intersection (common elements) of two or more arrays in php.

```
$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);
print_r(array_intersect($array1,$array2));
// Output:
Array
(
[1] => 3
[3] => 2
[4] => 1
)
```

When working with multiple arrays, it can be useful to find the entire collection of elements which exist in all of your arrays. The intersection of two or more arrays is the collection of elements which are included in all of the array.

So, for example, if we have an array A and an array B, then the intersection of A and B is a collection of elements which are in both A and B.

We can get the intersection of two arrays in php easily.

To find all of the common elements of a list of arrays in php, we can use the php **array_intersect()** function.

Below is a simple example of how to use **array_intersect()** to find the intersection of two arrays in php.

```
$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);
print_r(array_intersect($array1,$array2));
// Output:
Array
(
[1] => 3
[3] => 2
[4] => 1
)
```

## Finding the Intersection of Three or More Arrays with array_intersect() in php

The php **array_intersect()** function can find the intersection of more than two arrays.

The examples above only found the intersection between two arrays, but we can find the intersection of three or more arrays just as easy.

Below is an example of how to use **array_intersect()** to find the intersection of three arrays in php.

```
$array1 = array(5,3,8,2,1);
$array2 = array(9,3,4,2,1);
$array3 = array(0,9,8,1,3);
print_r(array_intersect($array1,$array2, $array3));
// Output:
Array
(
[1] => 3
[4] => 1
)
```

Hopefully this article has been useful for you to learn how to use **array_intersect()** to find the intersection of two or more arrays in php.

## Leave a Reply