W3docs

PHP Array Intersect_Assoc Function

The array_intersect_assoc function in PHP compares two or more arrays and returns only elements that match on both key and value in every array.

The PHP array_intersect_assoc() function compares two or more arrays and returns only the entries that match on both key and value in every array. Unlike array_intersect(), which ignores keys and looks at values alone, array_intersect_assoc() keeps a pair only when the same key holds the same value across all the arrays you pass in. That makes it the right tool when the position or label of a value matters as much as the value itself — for example, comparing two configuration arrays or two rows keyed by field name.

This page covers the function signature, how the comparison actually works (including a subtle point about how values are compared), several runnable examples, common gotchas, and related functions.

Syntax

array_intersect_assoc(array $array, array ...$arrays): array
  • $array — the base array. The result preserves its keys and value types.
  • ...$arrays — one or more arrays to compare against. You must pass at least one.

The function returns a new array containing every key => value pair from the first array whose key and value also appear in all of the other arrays.

How it works

array_intersect_assoc() walks through each key => value pair in the first array and looks for the same key in every other array. A pair survives only when, for every other array, that key exists and its value is equal.

One detail trips people up: the value comparison is not strict (===). Internally PHP compares values as strings, roughly (string) $a === (string) $b. So 1 (int) matches "1" (string) because both stringify to "1", but false does not match 0 (they stringify to "" and "0"). Keys, as always in PHP arrays, follow normal array-key rules.

Basic example

Here the only pair shared by both arrays — same key, same value — is "a" => "green":

php— editable, runs on the server

The output of this code would be:

Array
(
    [a] => green
)

"b" is dropped because the values differ (brown vs yellow), and "c" is dropped for the same reason (blue vs red). The bare "red" in $array1 has the integer key 0, which doesn't exist in $array2, so it is excluded too. This is the difference from array_intersect(), which would have matched red on value alone.

Comparing three or more arrays

A pair must match in all arrays to survive. Adding a third array narrows the result further:

<?php

$array1 = array("a" => "green", "b" => "brown", "c" => "blue");
$array2 = array("a" => "green", "b" => "yellow");
$array3 = array("a" => "green", "c" => "blue");
$result = array_intersect_assoc($array1, $array2, $array3);
print_r($result);

?>

Output:

Array
(
    [a] => green
)

Only "a" => "green" appears in every array. "c" => "blue" is missing from $array2, so it is discarded.

Gotcha: loose value comparison

Because values are compared as strings, mixed-type values can match in ways that surprise you:

<?php

$a = array("id" => 1,   "active" => false);
$b = array("id" => "1", "active" => 0);

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

?>

Output:

Array
(
    [id] => 1
)

id matches because 1 and "1" both stringify to "1". But active is dropped: false stringifies to "" while 0 stringifies to "0", so they are not equal. If you need strict type-aware matching, compare the arrays yourself or use array_uintersect_assoc() with a custom callback.

When to use it

  • Comparing associative arrays where a value is only meaningful at its specific key — settings, form fields, database-row-like structures.
  • Finding the common entries between two snapshots of the same keyed data.

If you only care about matching values regardless of key, use array_intersect(). If you only care about matching keys regardless of value, use array_intersect_key(). For the opposite operation — entries present in the first array but not the others, compared on key and value — see array_diff_assoc().

Diagram

A visual representation of how array_intersect_assoc() filters pairs:

graph LR
A[Array 1: key-value pairs] -->|Compare keys & values| B[Array 2: key-value pairs]
B -->|Keep only matching pairs| C[Result Array]

Conclusion

array_intersect_assoc() returns the key => value pairs that are common to all of the arrays you give it, matching on both the key and the (loosely string-compared) value. Reach for it when both the label and the value of an entry must agree, and prefer array_intersect(), array_intersect_key(), or array_uintersect_assoc() when you need value-only, key-only, or strict custom matching instead.

Practice

Practice
What does the array_intersect_assoc() function in PHP do?
What does the array_intersect_assoc() function in PHP do?
Was this page helpful?