array_diff_assoc

Returns entries of the first array whose key and value pair is not found in any other array.

Signature
array_diff_assoc(array $array, array ...$arrays): array
  • Added in PHP 4.3.0

Parameters

ParameterTypeDescription
$arrayarrayThe array to compare from.
$arraysoptionalarrayOne or more arrays to compare against (variadic).

Return value

arrayAn array of entries from $array whose key/value pair does not appear in any of the other arrays. Keys are preserved.

Errors

Values are cast to string for comparison, so array values trigger an "Array to string conversion" warning and objects without __toString() raise an error.

Changelog

  • PHP 8.0.0Can now be called with a single argument; previously at least two arrays were required.

array_diff_assoc() keeps an entry only when no other array has the same key holding the same value. Unlike array_diff(), which ignores keys, this one requires both halves of the pair to match before an entry is discarded.

When to reach for it

This is the change-detection function. Diff a submitted form against the record already in the database and you get exactly the fields that changed, ready to feed into an UPDATE or an audit log. Diff a runtime config against the shipped defaults and you get only the settings that were overridden. Diff yesterday's cached snapshot against today's and you get the deltas.

What trips people up

Values are compared as strings, so '1' and 1 look identical and 0 and false do not (they stringify to "0" and ""). Booleans are the usual source of surprise in config diffs.

It only reads the first dimension. If a value is itself an array, PHP tries to stringify it, emits an "Array to string conversion" warning, and treats every array value as the literal string "Array" — so all nested arrays compare as equal. Flatten first, or write a recursive diff.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Diff a submitted form against the stored record to build a minimal UPDATE statement
  • Show which config settings differ from the shipped defaults
  • Compare two API snapshots to log exactly which fields changed

Frequently asked questions

What is the difference between array_diff and array_diff_assoc?

array_diff matches on values alone and ignores keys, while array_diff_assoc requires the key and the value to match before an entry is removed. Use array_diff_assoc when the position or field name matters.

Does array_diff_assoc work with nested arrays?

No, it only compares the first dimension. Array values are cast to the string "Array", which makes them all compare as equal and raises an "Array to string conversion" warning.

Why is an entry missing from my diff even though the values differ?

Values are compared after casting to string, so 1 and '1', or 1.0 and '1', are considered identical. Use array_udiff_assoc() with a strict callback if you need type-aware comparison.

More examples

Key or value — either difference keeps the entry

array_diff_assoc keeps 'port' (same key, different value) and 'ssl' (key absent). Plain array_diff keeps only the values 5432 and 'on' by value alone — the same result here, but it would wrongly drop a value that exists under a different key.

Example · php
<?php
$a = ['host' => 'db1', 'port' => 5432, 'ssl' => 'on'];
$b = ['host' => 'db1', 'port' => 3306];

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

String comparison bites booleans

'n' is dropped because 1 and '1' both stringify to "1". 'flag' survives because 0 becomes "0" while false becomes "" — a difference that === would report but == would not.

Example · php
<?php
$a = ['n' => 1, 'flag' => 0, 'label' => 'x'];
$b = ['n' => '1', 'flag' => false, 'label' => 'x'];

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

Both directions for a full change set

One call cannot tell you what was removed. Running it both ways shows b changed and d was added, while the reverse shows b's old value and that c disappeared.

Example · php
<?php
$old = ['a' => 1, 'b' => 2, 'c' => 3];
$new = ['a' => 1, 'b' => 9, 'd' => 4];

print_r(['changed_or_added' => array_diff_assoc($new, $old),
         'changed_or_removed' => array_diff_assoc($old, $new)]);

Discussion

  • Be the first to comment on this lesson.