Menu Home

array_intersect

osbo.com | PHP | Functions | array_intersect

Computes the intersection of arrays

Syntax

array_intersect ( array $array1 , array $array2 [, array $... ] ) : array

Parameters

array1

The array with master values to check.

array2

An array to compare values against.

...

A variable list of arrays to compare.

Return

Returns an array containing all of the values in array1 whose values exist in all of the parameters.

Examples

array1 array2

<?

$array1 = array("a" => 0, "b" => 1, "c" => 2, "d" => 3);
$array2 = array("a" => 0, "b" => 4, "e" => 2, "f" => 5);

$return = array_intersect($array1, $array2);

print_r($return);

?>
Array
(
    [a] => 0
    [c] => 2
)

...

<?

$array1 = array("a" => 0, "b" => 1, "c" => 2, "d" => 3);
$array2 = array("a" => 0, "b" => 4, "e" => 2, "f" => 5);
$array3 = array("a" => 0, "b" => 4, "e" => 2, "f" => 5);

$return = array_intersect($array1, $array2, $array3);

print_r($return);

?>
Array
(
    [a] => 0
    [c] => 2
)

PHP