PHP Intersect Function: A Guide to array_intersect
PHP is a widely used programming language for web development, and its built-in functions make it easy to perform common tasks. One such function is the
The array_intersect() function compares two or more arrays and returns the values they all have in common. It is one of PHP's most useful array set operations — handy whenever you need to answer "which items appear in every list?", such as finding shared tags, common permissions, or products that exist in two catalogs.
This chapter covers the syntax, how comparison and key preservation actually work, the gotchas that trip people up (loose comparison, only values being matched), and runnable examples.
Syntax
array_intersect(array $array, array ...$arrays): array$array— the array whose values are checked against all the others....$arrays— one or more additional arrays to compare against.
The function returns a new array containing every entry from the first array whose value is present in all of the other arrays. The original arrays are left unchanged.
Two key details to remember:
- Only values are compared, not keys. Keys are ignored when matching, but the keys from the first array are preserved in the result.
- Comparison is loose by default. Internally PHP compares elements as strings:
(string) $a === (string) $b. So the integer1and the string"1"are treated as equal. Usearray_intersect_assoc()when keys must match too, orarray_intersect_key()to intersect by keys instead of values.
If any argument is an empty array, the result is always empty — nothing can be common to a list that has no elements.
Examples
Example 1: Intersecting two arrays
Compare two arrays and keep only the values found in both.
Output:
Array
(
[2] => c
[3] => d
)"c" and "d" appear in both arrays. Notice the keys: 2 and 3 come from $array1, the first argument — array_intersect() keeps the original keys rather than renumbering. If you need a clean 0, 1, 2 … index, wrap the result in array_values().
Example 2: Intersecting multiple arrays
You can pass any number of arrays. A value is only kept if it appears in every one of them.
Output:
Array
(
)The result is empty: no single value is shared by all three arrays. "c"/"d" are missing from $array3, and "e"/"f" are missing from $array1.
Example 3: Loose comparison gotcha
Because elements are compared as strings, numbers and their string equivalents match. This is the behaviour that surprises people most.
<?php
$numbers = array(1, 2, 3, 4);
$strings = array("2", "4", "6");
$result = array_intersect($numbers, $strings);
print_r($result);
?>Output:
Array
(
[1] => 2
[3] => 4
)The integer 2 matched the string "2", and 4 matched "4" — even though they are different types. The values returned come from the first array, so they remain integers here. If exact type matching matters, filter the result yourself or compare with stricter logic.
When to use it
Reach for array_intersect() when you want the common values across lists. A few practical cases:
- Find roles a user has in both of two permission sets.
- Show products that exist in two store inventories.
- Check which requested fields are actually allowed (
array_intersect($requested, $whitelist)).
For the opposite operation — values that are not shared — see array_diff(). To combine arrays instead of intersecting them, use array_merge(), and to test for a single value use in_array().
Conclusion
array_intersect() returns the values present in every array you give it, preserving the keys of the first argument and comparing elements loosely as strings. Remember to reach for array_intersect_assoc() or array_intersect_key() when keys matter, and array_values() when you want the result re-indexed.