PHP array_diff_assoc Function
last modified March 13, 2025
The PHP array_diff_assoc
function compares arrays and returns
differences with key checks. It's useful for finding mismatches in both
keys and values.
Basic Definition
The array_diff_assoc
function compares arrays and returns
differences. It checks both keys and values, unlike array_diff
.
Syntax: array_diff_assoc(array $array1, array $array2, ...): array
.
It returns elements from $array1 not present in other arrays. Comparison is
strict (===).
Basic array_diff_assoc Example
This shows simple comparison of two arrays with both key and value checks.
<?php $array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $array2 = ["a" => "apple", "b" => "blueberry", "c" => "cherry"]; $result = array_diff_assoc($array1, $array2); print_r($result);
This compares two arrays and finds differences. The output will be:
Array ( [b] => banana )
because the "b" key has different
values in both arrays.
Comparing Multiple Arrays
array_diff_assoc
can compare more than two arrays at once.
<?php $array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $array2 = ["a" => "apple", "b" => "banana"]; $array3 = ["a" => "apple", "c" => "cherry"]; $result = array_diff_assoc($array1, $array2, $array3); print_r($result);
This compares three arrays. The output will be empty because all elements in $array1 exist in at least one of the other arrays with same keys/values.
Numeric Key Comparison
The function works with numeric keys just like with string keys.
<?php $array1 = [1 => "one", 2 => "two", 3 => "three"]; $array2 = [1 => "one", 2 => "TWO", 4 => "four"]; $result = array_diff_assoc($array1, $array2); print_r($result);
This compares arrays with numeric keys. The output will be:
Array ( [2] => two [3] => three )
because key 2 has different
values and key 3 doesn't exist in $array2.
Type-Sensitive Comparison
array_diff_assoc
uses strict comparison (===) for both keys and values.
<?php $array1 = ["a" => "1", "b" => 2, "c" => 3.0]; $array2 = ["a" => 1, "b" => 2, "c" => 3]; $result = array_diff_assoc($array1, $array2); print_r($result);
This demonstrates strict comparison. The output will be:
Array ( [a] => 1 [c] => 3.0 )
because "1" (string) !== 1 (int)
and 3.0 (float) !== 3 (int).
Complex Array Comparison
The function can handle complex arrays with nested structures.
<?php $array1 = [ "fruit" => ["a" => "apple", "b" => "banana"], "color" => "red" ]; $array2 = [ "fruit" => ["a" => "apple", "b" => "blueberry"], "color" => "red" ]; $result = array_diff_assoc($array1, $array2); print_r($result);
This compares multidimensional arrays. The output will be empty because the function doesn't recursively compare nested arrays. Only top-level differences are detected.
Best Practices
- Key Importance: Use when key-value pairs must match exactly.
- Performance: Be mindful with large arrays as it's O(n*m).
- Type Safety: Remember it uses strict comparison.
- Nested Arrays: Combine with array_diff for deep comparison.
Source
PHP array_diff_assoc Documentation
This tutorial covered the PHP array_diff_assoc
function with
practical examples showing its usage for array comparison scenarios.
Author
List all PHP Array Functions.