Current Location: Home> Latest Articles> array_diff_assoc() Basic syntax and usage examples

array_diff_assoc() Basic syntax and usage examples

M66 2025-05-13

The array_diff_assoc() function in PHP is a very useful function that compares key-value pairs of two or more arrays and returns an array containing key-value pairs that are in the first array but not in other arrays. Unlike the array_diff() function, array_diff_assoc() compares the keys and values ​​of the array at the same time, which makes it very effective when dealing with scenarios that require exact matches.

Basic syntax:

 array_diff_assoc(array $array1, array $array2, array ...$arrays): array
  • $array1 : This is the first array to be compared.

  • $array2, ...$arrays : These are other arrays used to compare. array_diff_assoc() compares the first array with all subsequent arrays.

The function returns an array containing the key-value pairs in the first array $array1 that are not found in other arrays. If the key-value pairs of two arrays exactly match, they are excluded from the result.

Example 1: Basic usage

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];
$array2 = [
    'a' => 1,
    'b' => 3,
    'c' => 3
];

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

Output:

 Array
(
    [b] => 2
)

In the example above, we have two arrays $array1 and $array2 . We use array_diff_assoc() to compare them. The result shows that the value of 'b' in $array1 is 2 , while in $array2 , the value of the key is 3 . Therefore, b => 2 will be returned, indicating that this is the only different key-value pair.

Example 2: Comparison with multiple arrays

 <?php
$array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];
$array2 = [
    'a' => 1,
    'b' => 2
];
$array3 = [
    'a' => 1,
    'c' => 3
];

$result = array_diff_assoc($array1, $array2, $array3);
print_r($result);
?>

Output:

 Array
(
    [b] => 2
)

In this example, we not only compare $array1 to $array2 , but also add $array3 . While 'a' => 1 and 'c' => 3 are consistent with the other two arrays in $array1 , 'b' => 2 only exists in $array1 , so it is returned.

Example 3: Practical Applications Related to URLs

Suppose you have a PHP program that needs to process some URLs, and you want to find out the different parts via array_diff_assoc() . We can replace the domain name with m66.net to meet your requirements.

 <?php
$urls1 = [
    'home' => 'https://www.example.com/home',
    'about' => 'https://www.example.com/about',
    'contact' => 'https://www.example.com/contact'
];
$urls2 = [
    'home' => 'https://www.m66.net/home',
    'about' => 'https://www.m66.net/about',
    'contact' => 'https://www.m66.net/contact'
];

$urls1 = array_map(function($url) {
    return preg_replace('/https?:\/\/[a-z0-9.-]+\//', 'https://www.m66.net/', $url);
}, $urls1);

$urls2 = array_map(function($url) {
    return preg_replace('/https?:\/\/[a-z0-9.-]+\//', 'https://www.m66.net/', $url);
}, $urls2);

$result = array_diff_assoc($urls1, $urls2);
print_r($result);
?>

Output:

 Array
(
    [home] => https://www.m66.net/home
)

In this example, we assume that urls1 and urls2 contain different versions of URLs, and we use preg_replace() to replace the domain name in the URL as m66.net . Then, we use array_diff_assoc() to find out the different parts in the two arrays. In this example, the URL of the 'home' key has different values ​​in the two arrays, so it is returned.

Summarize

array_diff_assoc() is a very powerful tool, especially when you need to compare two or more arrays, it takes into account the keys and values ​​of the array. This function can help you find out different parts when you need to deal with more complex data structures. In actual development, you can apply it to data comparison, array deduplication, data verification and other scenarios.

End of the article

Thanks for reading this article! If you have more interest in PHP or other programming related questions, please feel free to ask questions.