array_intersect

Returns the values of the first array that are also present in every other array passed to it.

Signature
array_intersect(array $array, array ...$arrays): array
  • Added in PHP 4.0.1

Parameters

ParameterTypeDescription
$arrayarrayThe array with the master values to check.
$arraysoptionalarrayOne or more arrays to compare the values against.

Return value

arrayAn array of values from $array that are present in all of the other arrays, with the original keys preserved.

Errors

Passing a non-array argument throws a TypeError.

Changelog

  • PHP 8.0.0Can now be called with a single argument; previously at least two arrays were required.

array_intersect() answers "which of these values appear everywhere?" It compares values only — keys are ignored during comparison but the keys from the first array are carried into the result.

Comparison is string-based

Two elements are considered equal when (string) $a === (string) $b. That means 1 matches "1", and true matches "1" as well, because both stringify to "1". It also means objects without __toString() and nested arrays will trigger errors or notices rather than compare sensibly. For anything but scalars, map to a comparable key first or use array_uintersect() with your own comparator.

Duplicates and order

Duplicates in the first array are all kept if the value matches; duplicates in the later arrays make no difference. The result follows the order of the first array, not any of the others.

Choosing the right variant

Reach for array_intersect_key() when you care about keys and not values (field whitelisting), array_intersect_assoc() when both must match (diffing two records), and plain array_intersect() for two flat lists of scalars.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Keep only the request fields that appear in your list of updatable columns.
  • Find which tags two articles have in common.
  • Check which of a user's requested permissions their role actually grants.

Frequently asked questions

What is the difference between array_intersect and array_intersect_key?

array_intersect compares values and ignores keys; array_intersect_key compares keys and ignores values. Use the key version for whitelisting fields by name.

Does array_intersect preserve keys?

Yes, the keys from the first array are kept, so the result may not be a sequential list. Call array_values() on it if you need one.

Why does array_intersect match 1 and "1"?

Because elements are compared as strings using (string) $a === (string) $b. If you need type-strict matching, use array_uintersect() with a comparator that compares with <=> on identical types.

More examples

Intersecting more than two arrays

A value must be present in every argument to survive, so only ada and grace remain. linus missed Tuesday and rasmus missed Monday.

Example · php
<?php
$mon = ['ada', 'linus', 'grace', 'alan'];
$tue = ['grace', 'ada', 'rasmus'];
$wed = ['ada', 'grace', 'linus'];

$everyDay = array_intersect($mon, $tue, $wed);
print_r(array_values($everyDay));
echo count($everyDay), " people attended all three days\n";

Duplicates in the first array are kept

Both copies of 'pen' survive because duplicates in the first array are preserved. Wrapping the result in array_unique() collapses them when you want a distinct set.

Example · php
<?php
$cart = ['pen', 'pen', 'book', 'lamp'];
$inStock = ['pen', 'book'];

print_r(array_intersect($cart, $inStock));
print_r(array_values(array_unique(array_intersect($cart, $inStock))));

Discussion

  • Be the first to comment on this lesson.