W3docs

PHP array_diff_assoc function

The PHP function array_diff_assoc is used to compare two or more arrays and return the differences between them based on their keys. This function is

The PHP array_diff_assoc() function compares two or more arrays and returns the entries from the first array that are missing from all of the others. Unlike array_diff(), which looks only at values, array_diff_assoc() checks both the key and the value — so a pair survives the comparison only when no other array has that exact key paired with that exact value. This makes it the right tool for diffing associative arrays.

This page covers the syntax, how the key + value matching works, the difference from array_diff(), the loose (string) comparison gotcha, and a multi-array example.

Syntax

array_diff_assoc(array $array, array ...$arrays): array
  • $array — the array to compare from. Its surviving entries form the result.
  • ...$arrays — one or more arrays to compare against.

The function returns a new array containing every key/value pair of $array that does not appear (with the same key) in any of the other arrays. Keys are preserved in the result.

How it works

array_diff_assoc() walks the first array and, for each pair, asks: does any other array hold this same key with this same value? If yes, the pair is dropped; if no, it is kept.

PHP array_diff_assoc() example

php— editable, runs on the server

Output:

Array
(
    [b] => banana
)

Only "b" => "banana" survives: $array2 has no b key at all. The a and c pairs match by both key and value, so they are removed. Note that "d" => "date" in $array2 is irrelevant — only entries from the first array can appear in the result.

array_diff_assoc() vs array_diff()

The key difference is whether keys are part of the comparison. With the same value under different keys, array_diff() treats them as equal, but array_diff_assoc() does not:

<?php

$a = array("a" => "apple", "b" => "banana");
$b = array("x" => "apple");

print_r(array_diff_assoc($a, $b)); // compares key AND value
print_r(array_diff($a, $b));       // compares value only

?>

Output:

Array
(
    [a] => apple
    [b] => banana
)
Array
(
    [b] => banana
)

array_diff_assoc() keeps "a" => "apple" because $b stores apple under key x, not a. array_diff() drops it because the value apple exists somewhere in $b.

Gotcha: comparison is done as strings

Internally array_diff_assoc() compares values loosely by casting them to strings — (string) $elem1 === (string) $elem2. That means 10 (int) and "10" (string) are considered equal:

<?php

$a = array("x" => 10);
$b = array("x" => "10");

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

?>

Output:

Array
(
)

The result is empty: 10 and "10" stringify to the same "10". If you need strict type-aware comparison, write a callback with array_udiff_assoc() (or compare types yourself). For diffing on keys alone, use array_diff_key().

Comparing more than two arrays

You can pass any number of arrays. An entry must be absent from all of them to survive. This also works with numeric keys:

<?php

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");

print_r(array_diff_assoc($array1, $array2));

?>

Output:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

"a" => "green" matches and is removed. [0] => "red" survives because in $array2 the value "red" sits at key 1, not 0 — so the key/value pair differs.

When to use it

  • Diffing two configuration or settings arrays where the key matters (e.g. spotting changed options).
  • Finding entries that were removed from a "before" snapshot when both key and value identity matter.
  • Any comparison where matching only on value (array_diff()) would give false positives.

Practice

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