Arrays7 min readExamples run on PHP 8.5.10

array_intersect_assoc()

Learn how array_intersect_assoc() keeps entries of the first array only when both key and value match in every other array, with PHP 8.4 examples and pitfalls.

Signature

array_intersect_assoc(array $array, array ...$arrays): array
Available since PHP 4.3.07.48.08.18.28.38.48.5All versions →

Purpose

array_intersect_assoc() finds the entries that two or more arrays have in common, where an entry is a key and value pair. An entry from the first array survives only if every other array contains the same key with an equal value. This makes it stricter than array_intersect(), which looks at values alone and ignores keys.

Typical uses are comparing configuration arrays, checking which fields of a record are unchanged, and testing whether a set of expected settings is present in a larger set.

Signature

Parameters and defaults

  • $array: the master array. Its entries are the candidates, and the result is built from it.
  • ...$arrays: any number of further arrays to compare against. There are no default values.

Since PHP 8.0.0 the function may be called with a single argument. Earlier versions required at least two. With one argument, nothing can be filtered out, so you get the master array back.

Return value and failure cases

The function returns an array containing the entries of $array whose key and value are present in all the other arrays. Keys are preserved and the order of the first array is kept. If nothing matches, the result is an empty array.

The function does not return false on failure. Passing something that is not an array, such as a string or null, throws a TypeError because the parameters are typed.

How values are compared

Two entries count as equal only when their keys match and (string) $value1 === (string) $value2 is true. Both values are converted to strings and then compared strictly. As a result, the integer 3306 and the string "3306" are considered the same, while "1.0" and "1" are different.

Example 1: keys matter

The first example shows why the key is part of the comparison. Only the pair "a" => "green" appears in both arrays. The value "red" exists in both, but under different positional keys, so it is dropped. The key "b" exists in both but holds different values.

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

Example 2: comparing settings across several arrays

You can pass more than two arrays. An entry must match in all of them. The order of keys in the later arrays does not matter, and the result keeps the original value types from the first array.

Example
<?php
$defaults = ["host" => "localhost", "port" => 3306, "debug" => false, "charset" => "utf8"];
$env = ["host" => "localhost", "port" => "3306", "debug" => false, "charset" => "latin1"];
$override = ["port" => 3306, "host" => "localhost", "debug" => false];
$same = array_intersect_assoc($defaults, $env, $override);
echo json_encode($same, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"host":"localhost","port":3306,"debug":false}

The charset entry is removed because "utf8" and "latin1" differ and the third array lacks it. The port entry stays even though one array stores it as a string, because the string forms are identical.

Example 3: string conversion and gaps in list keys

Because the comparison goes through string conversion, true becomes "1" and null becomes an empty string. Also, when a list loses entries, the surviving numeric keys are kept, so the result may not be a list anymore.

Example
<?php
$x = ["a" => 1, "b" => true, "c" => null];
$y = ["a" => "1", "b" => "1", "c" => ""];
echo json_encode(array_intersect_assoc($x, $y), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";

$list1 = [1, 2, 3, 4];
$list2 = [1, 9, 3];
echo json_encode(array_intersect_assoc($list1, $list2), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"a":1,"b":true,"c":null}
{"0":1,"2":3}

The second result has keys 0 and 2. Because the keys are not a sequence starting at zero, json_encode() writes an object instead of an array. Use array_values() if you need to renumber.

Edge cases

  • Single argument: returns the first array unchanged in content.
  • Empty array among the arguments: the result is empty, since nothing can match.
  • Numeric string keys: PHP normalizes keys like "5" to the integer 5, so they behave as you would expect for array keys.
  • Nested arrays as values: converting an array to a string produces the text Array and a warning. Do not use this function to compare multidimensional data; compare nested values with a recursive approach of your own.
  • Floats: they are compared by their string form, so rounding differences in the printed value matter.

Common mistakes

  1. Expecting value-only matching. If keys differ, equal values will not match. Use array_intersect() when keys are irrelevant.
  2. Expecting strict type comparison. 1, "1" and true all convert to the same string, so they are treated as equal.
  3. Assuming the result comes from the last array. Keys and values always come from the first argument.
  4. Forgetting preserved keys. A filtered list keeps its original indexes, which can surprise code that loops with a counter.
  5. Using it on nested data. Arrays inside arrays trigger string conversion warnings and unreliable results.
  • array_intersect(): compares values only.
  • array_intersect_key(): compares keys only.
  • array_diff_assoc(): the opposite, returning entries of the first array that are missing elsewhere.
  • array_uintersect_assoc(): compares values with a callback.
  • array_intersect_uassoc(): compares keys with a callback.
  • array_uintersect_uassoc(): uses callbacks for both values and keys.

Recap

array_intersect_assoc() keeps entries of the first array when the same key holds a value with the same string form in every other array. It accepts one or more extra arrays, preserves keys and order, returns an empty array when nothing matches, and throws a TypeError for non-array arguments.

Tested examples

Only the matching key and value pair is kept
<?php
$a = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
$b = ["a" => "green", "b" => "yellow", "blue", "red"];
echo json_encode(array_intersect_assoc($a, $b), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"a":"green"}
Comparing settings across three arrays
<?php
$defaults = ["host" => "localhost", "port" => 3306, "debug" => false, "charset" => "utf8"];
$env = ["host" => "localhost", "port" => "3306", "debug" => false, "charset" => "latin1"];
$override = ["port" => 3306, "host" => "localhost", "debug" => false];
$same = array_intersect_assoc($defaults, $env, $override);
echo json_encode($same, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"host":"localhost","port":3306,"debug":false}
String conversion and preserved list keys
<?php
$x = ["a" => 1, "b" => true, "c" => null];
$y = ["a" => "1", "b" => "1", "c" => ""];
echo json_encode(array_intersect_assoc($x, $y), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";

$list1 = [1, 2, 3, 4];
$list2 = [1, 9, 3];
echo json_encode(array_intersect_assoc($list1, $list2), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
Output
{"a":1,"b":true,"c":null}
{"0":1,"2":3}