When comparing PHP arrays, checking values alone isn't always enough. Sometimes you need to know whether both the keys and their corresponding values match.
For example, you might want to identify changed configuration settings, compare user records, or find differences between two versions of an associative array.
PHP provides the array_diff_assoc() function for these situations.
What Is array_diff_assoc() in PHP?
The array_diff_assoc() function compares one array against other arrays, checking both keys and values.
Its syntax is:
array_diff_assoc(
array $array,
array ...$arrays
): array
The parameters are:
$array— The original array to compare....$arrays— Additional arrays to compare against.
The function returns entries from the first array that don't have an identical key-and-value combination in any comparison array.
The original arrays remain unchanged, and the returned array preserves the original keys.
Example 1: Comparing Associative Arrays
Let's compare two arrays containing user information.
<?php
$user1 = [
"name" => "Alex",
"role" => "Editor",
"active" => "Yes"
];
$user2 = [
"name" => "Alex",
"role" => "Admin",
"active" => "Yes"
];
$result = array_diff_assoc($user1, $user2);
print_r($result);
Output:
Array
(
[role] => Editor
)
The name and active entries match in both arrays, so they're excluded.
The role values differ, so PHP includes the value from the first array in the result.
Notice that array_diff_assoc() doesn't return the new value or describe what changed. It only returns unmatched entries from the first array.
Example 2: Why Array Keys Matter
Unlike array_diff(), this function also compares keys.
<?php
$first = [
"primary" => "PHP",
"secondary" => "Python"
];
$second = [
"language1" => "PHP",
"secondary" => "Python"
];
print_r(array_diff_assoc($first, $second));
Output:
Array
(
[primary] => PHP
)
Although both arrays contain PHP, the associated keys are different.
As a result, primary => PHP is included in the difference.
This is also important for indexed arrays: identical values at different numeric indexes aren't considered matching entries.
Example 3: Comparing Multiple Arrays
You can compare the first array against several arrays in one call.
<?php
$settings = [
"theme" => "dark",
"language" => "en",
"timezone" => "UTC"
];
$groupA = ["theme" => "dark"];
$groupB = ["language" => "en"];
$result = array_diff_assoc(
$settings,
$groupA,
$groupB
);
print_r($result);
Output:
Array
(
[timezone] => UTC
)
PHP excludes an entry if the same key-and-value pair appears in any of the comparison arrays.
Practical Example: Detecting Configuration Changes
Imagine your application stores configuration settings in associative arrays.
You want to compare the current settings with an updated configuration to determine which values have changed or been added.
Create a file named compare-settings.php:
<?php
declare(strict_types=1);
$currentSettings = [
"site_name" => "Know Stuff",
"theme" => "light",
"items_per_page" => 10
];
$newSettings = [
"site_name" => "Know Stuff",
"theme" => "dark",
"items_per_page" => 20,
"language" => "en"
];
// Find updated or newly added entries.
$differences = array_diff_assoc(
$newSettings,
$currentSettings
);
// Display the results.
echo "<h2>Updated Settings</h2>";
foreach ($differences as $key => $value) {
$description = $key . ": " . $value;
echo "<p>" . htmlspecialchars(
$description,
ENT_QUOTES,
"UTF-8"
) . "</p>";
}
Browser output:
Updated Settings
theme: dark
items_per_page: 20
language: en
The function identifies three differences:
themechanged from light to dark.items_per_pagechanged from 10 to 20.languageis a new setting.
Because the updated configuration is the first argument, the result contains its new values.
If you reverse the arguments, you'll receive the previous values that no longer have matching entries in the updated configuration.
Important: This is a one-directional comparison. To detect settings that were completely removed, you must also compare the original array against the updated array.
How Does array_diff_assoc() Compare Values?
The function compares keys and uses string representations when comparing values.
For example, integer 10 and string "10" are considered equal because their string representations match.
<?php
$first = ["quantity" => 10];
$second = ["quantity" => "10"];
var_dump(array_diff_assoc($first, $second));
Output:
array(0) {
}
This means array_diff_assoc() isn't suitable when you need strict type-sensitive comparisons.
The function also compares only one level of an array. It isn't designed to recursively compare nested arrays or objects.
array_diff() vs array_diff_assoc() vs array_diff_key()
| Function | What It Compares |
|---|---|
array_diff() |
Values only. |
array_diff_assoc() |
Keys and values together. |
array_diff_key() |
Keys only. |
Use array_diff_assoc() when the relationship between a key and its value matters, such as comparing configuration records or flat associative data.
Common Mistakes
- Expecting a two-way comparison: Only unmatched entries from the first array are returned.
- Ignoring array keys: Both the key and value must match for an entry to be excluded.
- Expecting strict value types: Values are compared through their string representations.
- Comparing nested arrays: Use an appropriate recursive comparison method for multidimensional data.
- Expecting both old and new values: Run comparisons in both directions when you need both sides of a change.
Key Takeaways
array_diff_assoc()compares both array keys and values.- It returns unmatched entries from the first array.
- You can compare against multiple arrays.
- The original keys are preserved in the result.
- Values are compared using their string representations.
- Comparisons are directional and not recursive.
- The function is useful for identifying changes in flat associative arrays.
The array_diff_assoc() function provides a straightforward way to detect differences between PHP arrays when both keys and values matter. It's particularly helpful for configuration comparisons, record updates, and other operations involving structured key-value data.
For additional details, see the official PHP array_diff_assoc() documentation.