In PHP, array_diff_key() is a function for comparing two or more arrays. It compares the keys of the array, not the value. This function returns an array that is included in the first array but not in other arrays.
Understanding the behavior of array_diff_key() is especially important when we deal with nested arrays, especially when dealing with complex associative arrays.
The basic syntax of the array_diff_key() function is as follows:
array_diff_key(array $array1, array $array2, array ...$arrays): array
$array1 is the first array to be compared.
$array2, ...$arrays is other arrays to be compared with the first array.
The function returns a new array containing key-value pairs that exist in $array1 but do not exist in other arrays.
For ordinary non-necked arrays, array_diff_key() compares the keys as expected. For example:
$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['b' => 2, 'd' => 4];
$result = array_diff_key($array1, $array2);
print_r($result);
The output will be:
Array
(
[a] => 1
[c] => 3
)
array_diff_key() returns a key in $array1 that is not included in $array2 . Note that the comparison here is based only on the key name, not the value.
Things get more complicated when we pass nested arrays into array_diff_key() . Consider the following example of nested arrays:
$array1 = [
'a' => ['name' => 'John', 'age' => 25],
'b' => ['name' => 'Jane', 'age' => 30],
'c' => ['name' => 'Tom', 'age' => 22]
];
$array2 = [
'b' => ['name' => 'Jane', 'age' => 30],
'd' => ['name' => 'Alex', 'age' => 28]
];
$result = array_diff_key($array1, $array2);
print_r($result);
The output will be:
Array
(
[a] => Array
(
[name] => John
[age] => 25
)
[c] => Array
(
[name] => Tom
[age] => 22
)
)
In this example, array_diff_key() compares keys of outer arrays ( a , b , c ) and only checks whether the key exists in another array. When the key b appears in $array2 , it is excluded from $array1 . The inner nested arrays here (such as 'a' => ['name' => 'John', 'age' => 25] ) are not compared separately. We can only say that the keys a and c are reserved.
It is worth noting that array_diff_key() does not recursively compare keys of nested arrays. If you want deeper array keys, you need to use recursive functions or manually handle them. Here is an example of recursion:
function array_diff_key_recursive($array1, $array2) {
$result = array_diff_key($array1, $array2);
foreach ($result as $key => $value) {
if (is_array($value)) {
$result[$key] = array_diff_key_recursive($value, $array2);
}
}
return $result;
}
$array1 = [
'a' => ['name' => 'John', 'age' => 25, 'address' => ['city' => 'New York', 'zip' => '10001']],
'b' => ['name' => 'Jane', 'age' => 30],
'c' => ['name' => 'Tom', 'age' => 22]
];
$array2 = [
'b' => ['name' => 'Jane', 'age' => 30]
];
$result = array_diff_key_recursive($array1, $array2);
print_r($result);
The output will be:
Array
(
[a] => Array
(
[name] => John
[age] => 25
[address] => Array
(
[city] => New York
[zip] => 10001
)
)
[c] => Array
(
[name] => Tom
[age] => 22
)
)
In this recursive implementation, we also perform a recursive array_diff_key() comparison of nested arrays, ensuring that the internal keys can also be compared correctly.
array_diff_key() is mainly used to compare keys of arrays, not values of arrays.
For nested arrays, array_diff_key() will only compare outer keys, and inner arrays will not participate in comparison.
If you need to recursively compare the keys of nested arrays, you can do this by custom recursive functions.
In actual development, understanding the behavior of array_diff_key() and its application in nested arrays is very important for processing complex data structures. It helps us process data flexibly and provides accurate array structure for further operations.