Current Location: Home> Latest Articles> Is it feasible to use array_diff_assoc() to make a differential comparison of multidimensional arrays?

Is it feasible to use array_diff_assoc() to make a differential comparison of multidimensional arrays?

M66 2025-06-07

In PHP, the array_diff_assoc() function is used to compare the differences between two or more arrays, return elements in the first array that are not in other arrays, and compare key names and values. This is a very convenient function, especially suitable for comparison of one-dimensional arrays.

However, things get a little more complicated when we apply it to multidimensional arrays. This article will explore the application of array_diff_assoc() in multi-dimensional arrays and give some suggestions on how to deal with complex situations.

array_diff_assoc() function overview

The array_diff_assoc() function is used to compare the key names and key values ​​of two arrays. If a key name and value exist in the first array but does not exist in the other array, or exists but is different, the element will be returned.

grammar:

 array_diff_assoc(array $array1, array $array2, array ...$arrays): array
  • array1 : The first array.

  • array2 : The second array, which can compare multiple arrays.

  • Returns an array containing differences.

Example: Comparing one-dimensional arrays

 $array1 = [
    "a" => "apple",
    "b" => "banana",
    "c" => "cherry"
];
$array2 = [
    "a" => "apple",
    "b" => "blueberry",
    "d" => "date"
];

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

Output:

 Array
(
    [b] => banana
    [c] => cherry
)

The challenge of multidimensional arrays

When we use array_diff_assoc() to compare multi-dimensional arrays, the problem becomes more complicated, because this function only compares the keys and values ​​of the top-level array and cannot deeply compare the contents of the multi-dimensional array.

Example: Comparison of multidimensional arrays

Suppose we have two multidimensional arrays and we want to compare their differences:

 $array1 = [
    "fruit" => [
        "apple" => 3,
        "banana" => 2,
    ],
    "vegetable" => [
        "carrot" => 5,
        "cucumber" => 7
    ]
];

$array2 = [
    "fruit" => [
        "apple" => 3,
        "banana" => 3,
    ],
    "vegetable" => [
        "carrot" => 5,
        "cucumber" => 8
    ]
];

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

Output:

 Array
(
    [fruit] => Array
        (
            [banana] => 2
        )

    [vegetable] => Array
        (
            [cucumber] => 7
        )
)

As shown above, array_diff_assoc() can only compare the top-level structure of an array and cannot deeply compare nested elements in multi-dimensional arrays.

How to deal with complex situations in multidimensional arrays?

For multidimensional arrays, we need to customize the comparison function to recursively compare the differences of each element. We can handle complex situations by calling array_diff_assoc() recursively or in combination with other functions.

Solution: Recursively compare multi-dimensional arrays

We can write a recursive function that manually handles comparisons of multidimensional arrays. Here is an example of dealing with differences in multidimensional arrays:

 function array_diff_assoc_recursive($array1, $array2) {
    $diff = [];

    foreach ($array1 as $key => $value) {
        // If the element is an array,Then call recursively
        if (is_array($value)) {
            $diff[$key] = array_diff_assoc_recursive($value, $array2[$key] ?? []);
        } else {
            // If the value is different,Then add it to the difference array
            if (!isset($array2[$key]) || $array2[$key] !== $value) {
                $diff[$key] = $value;
            }
        }
    }

    return $diff;
}

$result = array_diff_assoc_recursive($array1, $array2);
print_r($result);

Output:

 Array
(
    [fruit] => Array
        (
            [banana] => 2
        )

    [vegetable] => Array
        (
            [cucumber] => 7
        )
)

With this approach, we are able to recursively compare key values ​​of multi-dimensional arrays and obtain accurate differences.

Summarize

Although array_diff_assoc() is a very useful function, it has certain limitations when dealing with multidimensional arrays. In order to compare the differences between multi-dimensional arrays, we need to achieve more precise comparisons through recursion. In complex application scenarios, writing custom recursive comparison functions is the best way to deal with differences in multi-dimensional arrays.