PHP array_intersect() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_intersect() returns values from the first array that also appear in every other array.

You use array_intersect() to find what two lists have in common, such as users who are in two groups or tags that two articles share. It only looks at values, never at keys.

Syntax

Call array_intersect() with the values you need. Start with this line.

PHP
$common = array_intersect($list1, $list2);
CallWhat it does
array_intersect([1, 2, 3, 4, 5], [2, 4, 6])Returns [1 => 2, 3 => 4], the shared values.
array_intersect($a, $b, $c)Returns the values of $a that are in both $b and $c.
array_intersect($a, [])Returns an empty array, because nothing can be shared with an empty array.

The keys of the first array are kept, so the result can have gaps. Use array_values() to re-index it. Values are compared as strings. The order of the result follows the first array.

Library declarations
PHP
array_intersect(array $array, array ...$arrays): array

Parameters

ParameterDescription
arrayRequired. The array to compare from. Its keys are kept in the result.
arraysOptional. One or more arrays to compare with. A value must exist in all of them to be kept.

Return Value

Return ValueDescription
arrayThe values of the first array that are present in every other array.

PHP Examples

Example 1 — Finding Common Values

Keep only the values of the first list that also exist in the second list.

PHP
<?php
$a = [1, 2, 3, 4, 5];
$b = [2, 4, 6];

print_r(array_intersect($a, $b));

How It Works

  1. array_intersect() checks each value of $a against $b.
  2. The values 2 and 4 exist in both arrays, so they are kept.
  3. The values 1, 3 and 5 are not in $b, so they are removed.
  4. The kept values keep their original keys 1 and 3.

Example 2 — String Comparison

Values are compared as strings. The number 1 matches '1', but letter case matters.

PHP
<?php
$a = ['1', '2', 'a'];
$b = [1, 'A'];

print_r(array_intersect($a, $b));

How It Works

  1. The string '1' and the integer 1 have the same string form, so they match.
  2. The value '2' does not exist in $b.
  3. 'a' and 'A' are different strings, so they do not match.
  4. Only the first item is kept, and it keeps its key 0 and its original value '1'.

Example 3 — Three Arrays at Once

With more than two arrays, a value is kept only if it appears in all of them.

PHP
<?php
$first  = ['a', 'b', 'c', 'd'];
$second = ['b', 'c', 'd'];
$third  = ['c', 'd', 'e'];

$common = array_intersect($first, $second, $third);

echo implode(',', array_keys($common)), PHP_EOL;
echo implode(', ', array_values($common)), PHP_EOL;

How It Works

  1. 'a' is missing from $second, so it is removed.
  2. 'b' is missing from $third, so it is removed.
  3. 'c' and 'd' are in all three arrays and keep their keys 2 and 3.
  4. array_values() gives a clean list, which implode() joins as 'c, d'.

Frequently Asked Questions

It returns the values of the first array that are present in all the other arrays. It shows what the arrays have in common.
No, only values. Use array_intersect_key() to compare keys, or array_intersect_assoc() to compare both keys and values.
Yes. The result keeps the keys of the first array. Use array_values() if you want a list that starts at 0.
As strings, so 1 and '1' are equal. The comparison is case sensitive, so 'a' and 'A' are different.
array_diff(). It returns the values of the first array that are not in the other arrays.

Next: array_intersect_assoc()

array_intersect_assoc() compares both the keys and the values of the first array with othe.

array_intersect_assoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful