Current Location: Home> Latest Articles> Comparison of multiple arrays simultaneously: array_diff_assoc() Multiple parameters usage techniques

Comparison of multiple arrays simultaneously: array_diff_assoc() Multiple parameters usage techniques

M66 2025-06-06

array_diff_assoc() is a very useful function in PHP. It not only compares the values ​​of two arrays, but also compares their key names (key-value pairs). Through this function, developers can easily find the differences in key names and values ​​between two or more arrays, and are often used in scenarios such as data comparison and difference analysis.

In this article, we will dive into the array_diff_assoc() function and explain how to use it to compare multiple arrays through multiple instances. Especially when multiple arrays are involved, how to use this function to find the difference more efficiently.

Basic usage of array_diff_assoc()

First, let's review the basic usage of array_diff_assoc() . The function takes two or more array parameters and returns an array of key-value pairs contained in the first array but not in the other arrays.

grammar:

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

  • array2 : The second array that needs to be compared with the first array.

  • You can provide multiple arrays for comparison, array_diff_assoc() returns a key-value pair that is included in array1 but not in other arrays.

Example:

 <?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");
$array2 = array("a" => "apple", "b" => "banana", "d" => "date");

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

Output:

 Array
(
    [c] => cherry
)

In this simple example, array_diff_assoc() returns an array containing only the key-value pairs unique to array1 ["c" => "cherry"] because this element does not have a corresponding key-value pair in array2 .

How to compare multiple arrays

If you want to compare multiple arrays at the same time, array_diff_assoc() can also be handled. After passing multiple arrays, PHP compares the first array with all other arrays, and the returned result contains only key-value pairs that exist in the first array but not in any other array.

Example:

 <?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");
$array2 = array("a" => "apple", "b" => "banana");
$array3 = array("a" => "apple", "d" => "date");

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

Output:

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

In this example, array_diff_assoc() compares three arrays and returns a result containing key-value pairs in array1 that are not in the other two arrays. In particular, the b key exists in the second array, but its value is consistent with the b key value in the first array, so banana is excluded, leaving only cherry .

Practical application: Find differences

Suppose we are developing an online shopping system and have two arrays representing two versions of the product list. We need to find out the differences between the old and new lists and see what updates are in the new product and what is out of the old product.

Example:

 <?php
$old_products = [
    "101" => "Apple iPhone 12",
    "102" => "Samsung Galaxy S20",
    "103" => "Huawei P40"
];

$new_products = [
    "101" => "Apple iPhone 13",
    "102" => "Samsung Galaxy S20",
    "104" => "Google Pixel 5"
];

// Find out products that are not on the new list
$discontinued_products = array_diff_assoc($old_products, $new_products);
print_r($discontinued_products);
?>

Output:

 Array
(
    [103] => Huawei P40
)

In this example, the array_diff_assoc() function returns an array containing the discontinued products. As you can see, Huawei P40 is a product that exists in the old product list but disappears in the new one.

URL replacement application scenario

Sometimes in a project, we may need to batch modify the URL in the array, such as changing all domain names from oldsite.com to m66.net . We can quickly handle this situation by combining the array_diff_assoc() function and regular expression.

Example:

 <?php
$links = [
    "homepage" => "https://oldsite.com/page1",
    "product" => "https://oldsite.com/product/xyz",
    "contact" => "https://oldsite.com/contact"
];

// Replace domain name
foreach ($links as $key => $url) {
    if (strpos($url, "oldsite.com") !== false) {
        $links[$key] = str_replace("oldsite.com", "m66.net", $url);
    }
}

print_r($links);
?>

Output:

 Array
(
    [homepage] => https://m66.net/page1
    [product] => https://m66.net/product/xyz
    [contact] => https://m66.net/contact
)

In this way, we can batch replace all URL domain names in the array.

summary

Through this article, we understand how to use the array_diff_assoc() function in PHP to compare differences between multiple arrays. Whether it is a simple comparison of two arrays or a complex comparison involving multiple arrays, array_diff_assoc() can provide an effective solution. Especially in actual development, this function is a very practical tool when it is necessary to deal with tasks such as list comparison, data difference analysis, batch replacement of URL domain names, etc.