Arrays7 min readExamples run on PHP 8.5.10

array_intersect()

Learn how array_intersect() keeps values found in every array, preserves keys, compares values as strings, and where it commonly trips people up.

Signature

array_intersect(array $array, array ...$arrays): array
Available since PHP 4.0.17.48.08.18.28.38.48.5All versions →

Purpose

array_intersect() answers the question: which values of the first array also appear in every other array? It walks the first array and keeps each element whose value can be found in all of the remaining arrays. Typical uses are filtering a list of requested IDs against an allow-list, or finding the tags two records have in common.

Signature

Parameters and defaults

  • $array is the master array. Only its elements can appear in the result, and the result follows its order.
  • ...$arrays is a variadic list of arrays to compare against. There are no default values. Since PHP 8.0 the function may be called with just the master array; before that, at least two arguments were required.

Only values are compared. Keys of the other arrays are ignored, and so are their positions.

Return value and failure cases

The function returns an array containing every element of $array whose value exists in all of the other arrays. Keys from the master array are preserved, so the result often has gaps in its numeric keys. If nothing matches, you get an empty array.

Passing something that is not an array raises a TypeError. The function does not return false on failure.

How values are compared

Two elements are considered equal only when (string) $a === (string) $b. The comparison is therefore about string representations, not types. The integer 1, the string "1" and the boolean true all count as the same value, while "01" is different from "1".

Example 1: keys are preserved

The string key a and the integer key 0 both survive because the result keeps the keys of the first array.

Example
<?php
$a = ["a" => "green", "red", "blue"];
$b = ["b" => "green", "yellow", "red"];
echo json_encode(array_intersect($a, $b), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"a":"green","0":"red"}

The key b in the second array plays no role; only its value green matters.

Example 2: filtering IDs and reindexing

Because keys are kept, the filtered list is no longer a sequential list. Wrap the result in array_values() when you need keys 0, 1, 2... again, for instance before encoding as a JSON array.

Example
<?php
$ids = [10, 20, 30, 40];
$allowed = [20, 40, 50];
$kept = array_intersect($ids, $allowed);
echo json_encode($kept), "\n";
echo json_encode(array_values($kept)), "\n";
Output
{"1":20,"3":40}
[20,40]

Example 3: string comparison, duplicates and several arrays

With more than two arrays, a value must be present in all of them. Duplicates in the master array are all kept when they match, and mixed types compare by their string form.

Example
<?php
$first = [1, "1", true, "01", 2, 2, "x"];
$second = ["1", 2];
$third = [2, "1", "y"];
echo json_encode(array_intersect($first, $second, $third)), "\n";
Output
{"0":1,"1":"1","2":true,"4":2,"5":2}

The integer 1, the string "1" and true all stringify to "1", so they match. "01" and "x" do not. Both copies of 2 stay in the result.

Edge cases

  • Empty arrays: if any compared array is empty, the result is empty.
  • Single argument: in PHP 8.0 and later, array_intersect($array) is allowed and simply returns the elements of $array, since there is nothing to exclude them.
  • Nested arrays: an array element cannot be turned into a useful string, so comparing arrays of arrays is unreliable and triggers an "Array to string conversion" warning. Compare such data with your own callback logic instead.
  • Objects: an object element can only be compared if it can be converted to a string, for example through __toString().
  • Order: the output order always follows the first array, regardless of the order in the others.

Common mistakes

  1. Expecting a reindexed list. Forgetting that keys are preserved leads to surprising JSON objects or missing index 0. Use array_values() when you need a list.
  2. Expecting strict type comparison. 1, "1" and true are equal here. If types matter, filter with array_filter() and in_array($v, $other, true) instead.
  3. Swapping the argument order. The first array decides which elements, keys and order appear in the result. array_intersect($a, $b) and array_intersect($b, $a) can differ when keys or duplicates differ.
  4. Expecting key matching. If you want to compare keys as well, use array_intersect_assoc(); to compare keys only, use array_intersect_key().
  5. Comparing nested data. Multi-dimensional arrays need a different approach.
  • array_intersect_assoc() compares both key and value.
  • array_diff() returns the values of the first array that are missing from the others, the opposite question.
  • array_diff_assoc() is the key-aware version of the difference.
  • array_values() renumbers the keys of the result.
  • in_array() tests a single value and supports strict comparison.

Recap

array_intersect() keeps the elements of the first array whose string-equal value appears in every other array. It preserves keys and ordering from the first array, returns an empty array when nothing matches, and throws a TypeError for non-array arguments. Remember to reindex with array_values() when you need a list, and choose a different tool when you need strict type or key comparison.

Tested examples

Keys are preserved from the first array
<?php
$a = ["a" => "green", "red", "blue"];
$b = ["b" => "green", "yellow", "red"];
echo json_encode(array_intersect($a, $b), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"a":"green","0":"red"}
Filtering IDs and reindexing with array_values()
<?php
$ids = [10, 20, 30, 40];
$allowed = [20, 40, 50];
$kept = array_intersect($ids, $allowed);
echo json_encode($kept), "\n";
echo json_encode(array_values($kept)), "\n";
Output
{"1":20,"3":40}
[20,40]
String comparison, duplicates and several arrays
<?php
$first = [1, "1", true, "01", 2, 2, "x"];
$second = ["1", 2];
$third = [2, "1", "y"];
echo json_encode(array_intersect($first, $second, $third)), "\n";
Output
{"0":1,"1":"1","2":true,"4":2,"5":2}