PHP array_diff_assoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_diff_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 does not appear in the others.

You use array_diff_assoc() when the position or name of a value matters, for example when you want to see which settings were changed from their defaults. A pair only counts as equal when the key and the value are both the same.

Syntax

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

PHP
$changed = array_diff_assoc($current, $defaults);
CallWhat it does
array_diff_assoc(['a' => 1], ['a' => 1])Returns an empty array, because the key and value both match.
array_diff_assoc(['a' => 1], ['b' => 1])Returns ['a' => 1], because the key differs.
array_diff_assoc(['a' => 1], ['a' => 2])Returns ['a' => 1], because the value differs.

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

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

Parameters

ParameterDescription
arrayRequired. The array to compare from.
arraysOptional. One or more arrays to compare against.

Return Value

Return ValueDescription
arrayThe key => value pairs of the first array that are not present with the same key in any other array.

PHP Examples

Example 1 — Comparing Keys and Values

Only entries with the same key and the same value are treated as equal.

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

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

How It Works

  1. 'a' => 'green' exists in both arrays with the same key and value, so it is removed.
  2. The keys 'b' and 'c' do not exist in $b, so they are kept.
  3. 'red' is at key 0 in $a, but key 0 in $b holds 'yellow', so it is kept.
  4. 'red' also appears in $b, but at key 1, and the key must match too.

Example 2 — array_diff() Versus array_diff_assoc()

The same data gives different results, because array_diff() ignores the keys.

PHP
<?php
$x = ['x' => 1];
$y = ['y' => 1];

echo count(array_diff($x, $y)), PHP_EOL;
print_r(array_diff_assoc($x, $y));

How It Works

  1. Both arrays contain the value 1, but under the different keys 'x' and 'y'.
  2. array_diff() only looks at values, finds 1 in both and returns an empty array.
  3. The count of that empty result is 0.
  4. array_diff_assoc() also needs the keys to match, so it keeps 'x' => 1.

Example 3 — Finding Changed Settings

Compare the current settings with the defaults to see only what was changed.

PHP
<?php
$defaults = ['theme' => 'light', 'lang' => 'en', 'debug' => false];
$current  = ['theme' => 'dark', 'lang' => 'en', 'debug' => false];

print_r(array_diff_assoc($current, $defaults));

How It Works

  1. $current is the array we compare from, and $defaults is the reference.
  2. 'lang' and 'debug' have the same key and value in both arrays, so they are removed.
  3. 'theme' has the same key but a different value, so it is kept.
  4. The result contains only the setting that the user changed.

Frequently Asked Questions

It returns the entries of the first array whose key and value pair is not found in the other arrays. Both the key and the value must match for an entry to be removed.
array_diff() compares only the values. array_diff_assoc() compares keys as well, so the same value stored under a different key is still reported as a difference.
As strings. This means 1 and '1' are equal. The check is not type strict.
Yes. The result keeps the keys of the first array, including string keys.
Yes. Pass as many extra arrays as you need. An entry is removed when the same key and value appear in any of them.

Next: array_diff_key()

array_diff_key() compares the keys of the first array with the keys of other arrays. It re.

array_diff_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