Definition
The array_intersect() function compares the values of two or more arrays and returns the matches. The keys are not considered in the comparison, only the values are checked.
Syntax
array_intersect(array1, array2, array3, ...)
Parameters
Parameter | Description |
---|---|
array1 |
Required. The array to compare from. |
array2 |
Required. An array to compare against. |
array3 |
Optional. More arrays to compare against. |
Example
<?php
// Indexed Arrays
$cities1 = array("New York", "Salt Lake", "Tokyo");
$cities2 = array("Washington", "London", "Tokyo");
$result = array_intersect($cities1, $cities2);
print_r($result);
echo "<br>";
// Associative Arrays
$ages1 = array("Mark" => 22, "Jeff" => 32, "Mike" => 28);
$ages2 = array("Fred" => 22, "John" => 32, "Robin" => 28);
$ages3 = array("Mark" => 23, "John" => 32, "Mike" => 36);
$result = array_intersect($ages1, $ages2);
print_r($result);
echo "<br>";
$result = array_intersect($ages1, $ages3);
print_r($result);
echo "<br>";
$result = array_intersect($ages1, $ages2, $ages3);
print_r($result);
echo "<br>";