Current Location: Home> Latest Articles> Use array_diff_assoc() and array_intersect_assoc() together

Use array_diff_assoc() and array_intersect_assoc() together

M66 2025-06-06

In PHP, array_diff_assoc() and array_intersect_assoc() are two very useful functions, respectively, used to find the difference and intersection of an array. They not only compare the values ​​of the array, but also the key names, so they compare key-value pairs more precisely. In this article, we will learn how to use these two functions in combination to implement the difference and intersection operations of an array.

1. Introduction to array_diff_assoc()

The array_diff_assoc() function returns the difference between two or more arrays. Unlike array_diff() , array_diff_assoc() not only compares the values ​​of the array, but also compares the key names. If a key name and the corresponding value are not equal in multiple arrays, it will be considered part of the difference set.

Function prototype :

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

Sample code :

 $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 above example, array_diff_assoc() returns only the value 2 corresponding to the key name "b" because it is different in $array1 and $array2 .

2. Introduction to array_intersect_assoc()

The array_intersect_assoc() function is used to return the intersection of two or more arrays. Unlike array_intersect() , array_intersect_assoc() will also compare key names. Only elements with the same value and key names will appear in the intersection.

Function prototype :

 array_intersect_assoc(array $array1, array $array2, array ...$arrays): array

Sample code :

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3
];

$array2 = [
    "a" => 1,
    "b" => 2,
    "d" => 4
];

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

Output :

 Array
(
    [a] => 1
    [b] => 2
)

In this example, array_intersect_assoc() returns the part where the key name and corresponding value are the same, i.e. "a" => 1 and "b" => 2 .

3. Combining array_diff_assoc() and array_intersect_assoc() to implement the difference and intersection operations

Assuming you need to find the intersection and difference from two arrays, you can use array_diff_assoc() and array_intersect_assoc() to achieve this goal.

Sample code :

 $array1 = [
    "a" => 1,
    "b" => 2,
    "c" => 3
];

$array2 = [
    "a" => 1,
    "b" => 3,
    "d" => 4
];

// Computational intersection
$intersect = array_intersect_assoc($array1, $array2);

// Calculate the difference set
$diff = array_diff_assoc($array1, $array2);

echo "Intersection:\n";
print_r($intersect);

echo "Difference:\n";
print_r($diff);

Output :

 Intersection:
Array
(
    [a] => 1
)

Difference:
Array
(
    [b] => 2
    [c] => 3
)

In this example, the intersection only contains "a" => 1 , and the difference contains elements with different key names and corresponding values, i.e. "b" => 2 and "c" => 3 .

4. Use scenarios

Using array_diff_assoc() and array_intersect_assoc() can be very convenient to handle arrays with key-value pairs, especially when it is necessary to accurately compare key-value pairs of arrays. Here are some common usage scenarios:

  • Compare two configuration files to find their common configuration items and different configuration items.

  • Compare the two data sets, find out their intersection and difference sets, and synchronize or merge the data.

  • When working with multi-dimensional arrays, make sure that not only the values ​​of the array, but also the key names.

5. Conclusion

By using array_diff_assoc() and array_intersect_assoc() , you can easily handle the difference and intersection operations of an array. It should be noted that both functions can compare key names and corresponding values, which makes them very useful when dealing with arrays with associated keys.