array_intersect()
array_intersect() — Computes the intersection of arrays.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
array_intersect() returns an array containing all the values of array that are present in all the arguments. Note that keys are preserved.
Signature
array_intersect(array $array, array ...$arrays): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $array | array | The array with master values to check. |
| ...$arrays | array | Arrays to compare values against. |
Return value
Returns an array containing all of the values in array whose values exist in all of the parameters.
Example
A runnable snippet:
$array1 = array("a" => "green", "red", "blue"); $array2 = array("b" => "green", "yellow", "red"); $result = array_intersect($array1, $array2); print_r($result);
Output:
Array ( [a] => green [0] => red )
Changelog
| Version | Change |
|---|---|
| 8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
See also
array_intersect_assoc(), array_diff(), array_diff_assoc()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_intersect() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.