PHP array_intersect_assoc() Function

Quick summary: The PHP array_intersect_assoc() function compares the values and keys of two (or more) arrays, and returns the matches.

PHP array_intersect_assoc() Syntax

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

PHP array_intersect_assoc() Basic examples

$a = ['a'=>1,'b'=>2];
$b = ['a'=>1,'b'=>3];
print_r(array_intersect_assoc($a,$b));
PHP
Output:
Array ( [a] => 1 )

Returns elements where both key and value match.

PHP array_intersect_assoc() Real-world usage

$permissions1=['read'=>true,'write'=>false];
$permissions2=['read'=>true,'write'=>true];
print_r(array_intersect_assoc($permissions1,$permissions2));
PHP
Output:
Array ( [read] => 1 )

Find shared permissions with same value.

PHP array_intersect_assoc() Edge cases

$a=['1'=>1];
$b=[1=>1];
print_r(array_intersect_assoc($a,$b));
PHP
Output:
Array ( [1] => 1 )

String and integer keys may be treated as equal.

PHP array_intersect_assoc() Common mistakes

Confusing with array_intersect()

array_intersect_assoc() compares keys and values.

Incorrect
array_intersect($a,$b);
Correct
array_intersect_assoc($a,$b);

Use correct function when keys matter.

PHP array_intersect_assoc() Frequently Asked Questions

What does array_intersect_assoc() do?

Computes intersection with index check.

Difference from array_intersect()?

Also compares keys.

Use case?

Strict matching.

Return type?

Array.

Common mistake?

Ignoring keys.

Performance?

Moderate.

Safe?

Yes.

Handles nested arrays?

No.

Alternative?

array_intersect().

Key sensitive?

Yes.

Best practice?

Use for strict comparisons.

Used in configs?

Yes.

PHP array_intersect_assoc() Related PHP Functions