PHP array_intersect() function

PHP array_flip() function
PHP array_intersect_assoc() function

Recall the intersection of two sets that you studied in your school life. Similarly, array_intersect compares values of two arrays and returns the common values.

This function can also intersect more than two arrays. The first array is compared with the other arrays and returns the common values i.e the values that are present in array 1 and also the other arrays given to these functions.

What is the syntax of the array_intersect function in PHP?

array_intersect(array1, array2, array3, ...)
ParametersDetails
array1Array to make comparison from – Required
array2Array to compare with – Required
array3,…Further arrays to compare with – Required
array_intersect function in PHP

Examples of array_intersect function

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_intersect($a1,$a2);
print_r($result);
?>

In the above example, the array_intersect function compares the values of two arrays and returns the matches.

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result=array_intersect($a1,$a2,$a3);
print_r($result);
?>

In the above example, the array_intersect function compares the values of three arrays and returns the matches.

PHP array_flip() function
PHP array_intersect_assoc() function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top