array_intersect_assoc
Returns the entries whose key and value both match in every array passed to it.
array_intersect_assoc(array $array, array ...$arrays): array- Added in PHP 4.3.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The array with the master values to check. |
$arraysoptional | array | One or more arrays to compare the key/value pairs against. |
Return value
arrayAn array containing the entries of $array whose key and value both appear in all of the other arrays.
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_assoc() is the stricter cousin of array_intersect(): an entry only survives if both its key and its value are found in every other array. It is the natural tool for asking "which fields of this record are identical to that one?"
Two comparisons, two rules
Values are compared as strings, exactly like array_intersect(), so 1 and "1" are treated as the same value. Keys are compared using PHP's normal array key semantics, which means the string key "7" and the integer key 7 are the same key because PHP normalises numeric string keys on insertion. Neither comparison is type strict, so this function is not a substitute for ===.
Records, not lists
On plain lists this function is usually not what you want: because positions must line up, ['a','b'] and ['b','a'] intersect to nothing. It shines on associative arrays such as database rows, config arrays and request payloads.
The mirror image
Pair it with array_diff_assoc(): the intersection is what stayed the same, the diff is what changed. Together they let you build a minimal update payload.
Example
When to use it
- Work out which fields of a submitted form are unchanged from the stored record.
- Compare an environment config against defaults to see what has been left at its default value.
- Verify that a cached row still matches the database row key for key.
Frequently asked questions
What is the difference between array_intersect and array_intersect_assoc?
array_intersect compares values only, while array_intersect_assoc requires the key and the value to match. Use the assoc version when the position or field name matters.
Is array_intersect_assoc type strict?
No. Values are compared by their string representation, so 1 and "1" are considered equal. There is no built-in strict variant; use array_uintersect_assoc() with your own comparator.
Why does array_intersect_assoc return an empty array for my two lists?
Because on a numeric list the index is the key, so the same value at a different position does not match. Either sort the lists first or use array_intersect().
More examples
Keys must match too
'blue' exists in both arrays but under different keys, so array_intersect_assoc drops it and returns only x => green. Plain array_intersect keeps both values.
<?php
$a = ['x' => 'green', 'y' => 'blue'];
$b = ['x' => 'green', 'z' => 'blue'];
print_r(array_intersect_assoc($a, $b)); // key and value must match
print_r(array_intersect($a, $b)); // value alone is enoughComparing three configs
Only locale is identical across all three environments, so it is the single surviving entry. The second line lists the dev keys that differ from prod: debug and cache.
<?php
$dev = ['debug' => true, 'cache' => false, 'locale' => 'en'];
$test = ['debug' => true, 'cache' => true, 'locale' => 'en'];
$prod = ['debug' => false, 'cache' => true, 'locale' => 'en'];
$sameEverywhere = array_intersect_assoc($dev, $test, $prod);
print_r($sameEverywhere);
print_r(array_keys(array_diff_assoc($dev, $prod)));
Discussion