Current Location: Home> Latest Articles> Can custom comparison functions be used for array_diff_assoc()? (No, explain the alternative in detail)

Can custom comparison functions be used for array_diff_assoc()? (No, explain the alternative in detail)

M66 2025-06-06

array_diff_assoc() is based on PHP's built-in comparison mechanism to determine whether two arrays are equal. It uses the standard PHP comparison operator ( == ). This function does not provide a parameter to pass into a custom comparison function, so you cannot directly change the way it compares.

Specifically, the implementation of the array_diff_assoc() function is not designed to support callback functions (such as usort() , array_map() and other functions to support incoming custom functions). This makes it impossible for us to customize custom comparison logic in array_diff_assoc() , such as comparing array values ​​by specific rules, or using specific rules when comparing arrays.

Standard way to compare two arrays in PHP

By default, array_diff_assoc() makes standard comparisons based on the keys and values ​​of the array. If two arrays have the same key but their values ​​are not the same, array_diff_assoc() returns the difference part. For example:

 $array1 = [
    "a" => "apple",
    "b" => "banana",
    "c" => "cherry"
];

$array2 = [
    "a" => "apple",
    "b" => "berry",
    "c" => "cherry"
];

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

Output:

 Array
(
    [b] => banana
)

In this example, array_diff_assoc() finds the difference between the two arrays: the value with the key "b" is different in the two arrays, so it returns the difference.

However, if you want to follow certain custom rules (such as comparing strings in some way, ignoring case, ignoring certain specific characters, etc.), array_diff_assoc() will not meet the needs.

Alternative: Custom comparison function implementation

Although array_diff_assoc() itself does not support custom comparison functions, we can implement custom comparison logic through other methods. A common solution is to manually implement the difference comparison via array_filter() and a custom comparison function.

Here is an example of replacing array_diff_assoc() with a custom comparison function:

 function custom_compare($value1, $value2) {
    // For example,Ignore case for comparison
    return strcasecmp($value1, $value2) !== 0;
}

function custom_array_diff_assoc($array1, $array2) {
    $result = [];

    foreach ($array1 as $key => $value1) {
        if (!array_key_exists($key, $array2) || custom_compare($value1, $array2[$key])) {
            $result[$key] = $value1;
        }
    }

    return $result;
}

$array1 = [
    "a" => "Apple",
    "b" => "banana",
    "c" => "Cherry"
];

$array2 = [
    "a" => "apple",
    "b" => "Banana",
    "c" => "Cherry"
];

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

Output:

 Array
(
    [a] => Apple
    [b] => banana
)

In this example, we define a custom comparison function called custom_compare() to ignore the upper and lower case of strings for comparison. In custom_array_diff_assoc() , we first check whether the key exists in both arrays. If it exists, we can judge whether the values ​​are the same through a custom comparison function. If the values ​​are different, the key-value pair is added to the result array.

This method gives us more flexibility and be able to customize comparison logic according to our needs.

Other common alternatives

In addition to manually implementing the comparison logic, you can also use other built-in functions such as array_udiff_assoc() , which allows you to use user-defined comparison functions to compare differences in arrays. The principle of the array_udiff_assoc() function is similar to array_diff_assoc() , but it accepts a custom callback function to make comparisons.

Sample code:

 function custom_compare($value1, $value2) {
    return strcasecmp($value1, $value2);
}

$array1 = [
    "a" => "Apple",
    "b" => "banana",
    "c" => "Cherry"
];

$array2 = [
    "a" => "apple",
    "b" => "Banana",
    "c" => "Cherry"
];

$result = array_udiff_assoc($array1, $array2, 'custom_compare');
print_r($result);

Output:

 Array
(
    [a] => Apple
    [b] => banana
)

In this example, the array_udiff_assoc() function uses custom_compare() to compare the differences between two arrays, allowing for more flexible comparison behavior.