PHP array_intersect_assoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_intersect_assoc() compares both the keys and the values of the first array with other arrays. It returns the entries of the first array whose key and value pair also appears in all of the others.

You use array_intersect_assoc() when the key matters as much as the value, for example to find settings that two configurations share or to see which positions hold the same value in two lists.

Syntax

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

PHP
$same = array_intersect_assoc($a, $b);
CallWhat it does
array_intersect_assoc(['a' => 1], ['a' => 1])Returns ['a' => 1], because the key and value both match.
array_intersect_assoc(['a' => 1], ['b' => 1])Returns an empty array, because the keys differ.
array_intersect_assoc(['a' => 1], ['a' => 2])Returns an empty array, because the values differ.

It is the strict partner of array_intersect(), which compares only values. Values are compared as strings, but the key must match exactly. The result keeps the keys and values of the first array.

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

Parameters

ParameterDescription
arrayRequired. The array to compare from.
arraysOptional. One or more arrays to compare with. An entry must match in all of them to be kept.

Return Value

Return ValueDescription
arrayThe key => value pairs of the first array that are present, with the same key and value, in all the other arrays.

PHP Examples

Example 1 — Matching Keys and Values

Only entries with the same key and the same value in both arrays are kept.

PHP
<?php
$a = ['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'];
$b = ['a' => 'green', 'b' => 'yellow', 'blue', 'red'];

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

How It Works

  1. 'a' => 'green' exists in both arrays with the same key and value, so it is kept.
  2. 'b' has the value 'brown' in $a but 'yellow' in $b, so it is removed.
  3. 'c' does not exist in $b at all.
  4. 'red' is at key 0 in $a but at key 1 in $b, and key 0 in $b holds 'blue', so it is removed too.

Example 2 — Values Are Compared as Strings

The string '1' and the integer 1 count as equal, and the result keeps the value from the first array.

PHP
<?php
$a = ['a' => '1'];
$b = ['a' => 1];

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

How It Works

  1. The key 'a' exists in both arrays.
  2. The values '1' and 1 have the same string form, so they match.
  3. The entry is kept in the result.
  4. var_export() shows '1' as a string, because the value comes from the first array.

Example 3 — Comparing Lists by Position

For lists, the key is the position. Compare array_intersect() and array_intersect_assoc() on the same data.

PHP
<?php
$a = ['red', 'green', 'blue'];
$b = ['blue', 'green', 'red'];

echo count(array_intersect($a, $b)), PHP_EOL;
print_r(array_intersect_assoc($a, $b));

How It Works

  1. Both lists contain the same three colors, only in a different order.
  2. array_intersect() ignores the keys, so all 3 values are found and the count is 3.
  3. array_intersect_assoc() needs the same key as well as the same value.
  4. Only 'green' is at position 1 in both lists, so it is the only entry kept.

Frequently Asked Questions

It returns the entries of the first array whose key and value pair is present in all the other arrays. Both the key and the value must match.
array_intersect() compares only values and ignores the keys. array_intersect_assoc() also needs the keys to be the same.
As strings. So 1 and '1' are equal. The check is not type strict, and the value from the first array is the one that is returned.
Yes. The result keeps the keys of the first array.
array_diff_assoc(). It returns the entries of the first array whose key and value pair is not found in the other arrays.

Next: array_intersect_key()

array_intersect_key() returns the items of the first array whose keys also exist in every .

array_intersect_key() 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