Example 1 — Matching Keys and Values
Only entries with the same key and the same value in both arrays are kept.
<?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
- 'a' => 'green' exists in both arrays with the same key and value, so it is kept.
- 'b' has the value 'brown' in $a but 'yellow' in $b, so it is removed.
- 'c' does not exist in $b at all.
- '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.

