array_diff_assoc() is a very useful function in PHP that compares two arrays, returning key-value pairs that are in the first array but not in the second array. Specifically, it not only compares the values of an array, but also the keys of the array. In this way, the array_diff_assoc() function is more precise than array_diff() because it takes into account the correspondence between key names and key values.
In this article, we will use graphical methods to help you better understand how the array_diff_assoc() function works. Let's start with a simple example and explain the behavior of this function step by step.
array_diff_assoc(array $array1, array $array2): array
<?php
$array1 = [
"a" => 1,
"b" => 2,
"c" => 3
];
$array2 = [
"a" => 1,
"b" => 4,
"d" => 5
];
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
Array
(
[b] => 2
[c] => 3
)
In this example, array_diff_assoc() compares array1 and array2 . Let's analyze how it works step by step.
Key a :
The values of key a in array1 and array2 are both 1 , so there is no difference between this pair of key-value pairs.
Key b :
In array1 , the value of key b is 2 , while in array2 , the value of key b is 4 . Because key values are inconsistent, b => 2 is considered to be a key-value pair unique to array1 and appears in the result.
Key c :
In array1 , the value of key c is 3 , while in array2 there is no key c . So c => 3 is also considered a key-value pair unique to array1 and appears in the result.
Therefore, array_diff_assoc() returns an array containing b => 2 and c => 3 .
In order to more intuitively understand the working principle of array_diff_assoc() , we can show the comparison process of two arrays through illustrations.
Suppose there are two arrays:
array1 : ["a" => 1, "b" => 2, "c" => 3]
array2 : ["a" => 1, "b" => 4, "d" => 5]
We can clearly see their differences through the following diagram.
array1: [ "a" => 1, "b" => 2, "c" => 3 ]
↑ ↑
array2: [ "a" => 1, "b" => 4, "d" => 5 ]
↑
Key a : same (both key and value match), so it is not in the result.
Key b : The keys are the same but the values are different. The difference is considered unique to array1 , so it appears in the result.
Key c : There is no key c in array2 . c => 3 is considered unique to array1 , so it also appears in the result.
Result: [ "b" => 2, "c" => 3 ]
array_diff_assoc() not only compares arrays by values, it also takes into account key matching. If there are the same keys in the two arrays but different values, array_diff_assoc() treats it as a difference.
If there is no key in array2 at all (such as c ), then the key-value pair is considered unique to array1 and will also appear in the result array.
Filtering data differences : When processing array data, array_diff_assoc() can be used to find differences in one array from another, especially if the keys and values of two arrays need to match exactly.
Processing configuration arrays : If you need to compare two configuration arrays and check if there are different configuration items, array_diff_assoc() is a very good choice.
Data deduplication : When you have two data sets, if you want to find the unique part of a data set, you can use array_diff_assoc() to achieve it.
array_diff_assoc() is a very practical function in PHP that can help us compare the differences between key-value pairs of two arrays and return an array containing differential key-value pairs. By comparing the examples of array1 and array2 , we understand how it judges the difference by comparing keys and values simultaneously.
If you have any questions, or would like to have a deeper understanding of other array manipulation functions in PHP, feel free to discuss with me!