PHP 8 reference · Array Functions
PHP array_intersect() Function
Return the values of the first array that are also present in all the other arrays. Keys come from the first array.
PHP 8 Since 4.0
Syntax
array_intersect(array $array, array ...$arrays): array
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
arrays |
Arrays to compare against. |
Return value
The common values.
Examples
<?php
print_r(array_intersect(["php", "sql", "css"], ["sql", "php", "go"]));
Output
Array
(
[0] => php
[1] => sql
)<?php
$ada = ["php", "python", "sql"];
$alan = ["sql", "go", "php"];
echo "shared skills: ", implode(", ", array_intersect($ada, $alan)), "\n";
Output
shared skills: php, sql
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
set.intersection()· Python
Related
array_intersect_key()· Return the entries of the first array whose keys exist in all the other arrays. Great for whitelisting fields.array_diff()· Return the values of the first array that are not in any of the others. Values are compared as strings.- PHP Array Functions · tutorial
- PHP Arrays · tutorial