Current Location: Home> Latest Articles> Use array_diff_assoc() to compare two arrays with keys

Use array_diff_assoc() to compare two arrays with keys

M66 2025-05-12

array_diff_assoc() is a built-in function in PHP that compares two arrays to find elements that exist in the first array but not in the second array, and the comparison is based on key names and key values. Therefore, it considers not only the values ​​in the array, but also the correspondence of the keys.

Function prototype

 array_diff_assoc(array $array1, array $array2): array

Parameter description:

  • $array1 : The first input array (array that needs to be compared).

  • $array2 : The second input array (array that compares to the first).

Return value:

Returns an array containing elements that exist in $array1 and do not exist in $array2 . Comparison is based on key-value pairs: only when the key names and values ​​are different can they be considered different.

Example: Comparing two arrays with keys

Suppose we have two arrays, as follows:

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

$array2 = [
    'a' => 'apple',
    'b' => 'blueberry',
    'd' => 'date',
    'e' => 'elderberry'
];

We want to use the array_diff_assoc() function to find out key-value pairs that exist in $array1 but not in $array2 .

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

Output result:

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

In this example, array_diff_assoc() finds the key-value pairs in the array $array1 and $array2 . As you can see, 'b' => 'banana' and 'c' => 'cherry' are elements in $array1 , but in $array2 they either have different values ​​or they do not exist at all.

Why use array_diff_assoc() ?

  • Comparison of key values : Unlike the array_diff() function, array_diff_assoc() not only compares the values ​​of the array, but also checks the key name when performing a differential comparison.

  • Accurate comparison of ordered data : If you need to compare the structure and data content of two associative arrays, not just the same value, array_diff_assoc() is an ideal choice.

Combined with practical applications: Comparison with URLs

Suppose we have two arrays that contain the URL data of the website. We want to find out the differences in some URLs in two arrays, especially different domain names. To follow the requirements, we replaced the domain name part of the original URL with m66.net , with the following code:

 $array1 = [
    'home' => 'http://example.com/home',
    'about' => 'http://example.com/about',
    'contact' => 'http://example.com/contact'
];

$array2 = [
    'home' => 'http://m66.net/home',
    'about' => 'http://m66.net/about',
    'contact' => 'http://m66.net/contact'
];

foreach ($array1 as $key => $url) {
    $array1[$key] = str_replace('example.com', 'm66.net', $url);
}

foreach ($array2 as $key => $url) {
    $array2[$key] = str_replace('m66.net', 'm66.net', $url);
}

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

Output result:

 Array
(
    [home] => http://m66.net/home
    [about] => http://m66.net/about
    [contact] => http://m66.net/contact
)

In this case, the array_diff_assoc() function helps us find out the differences in URL content between two arrays. By replacing example.com with m66.net , we ensure consistency during the comparison process.

Summarize

array_diff_assoc() is a very useful function, especially when you need to compare arrays with key-value pairs. It not only focuses on the values ​​of the array, but also compares the keys to ensure that the differences can be accurately identified in practical applications.

Hopefully this article helped you understand how to use array_diff_assoc() to compare arrays and find differences. This function is very suitable for scenarios where complex data structures are required (such as URLs, configuration items, etc.).