In daily PHP development, we often encounter situations where we need to compare the differences between arrays. For multidimensional arrays, array_diff_ukey() is a very practical tool if we only care about the differences in the top-level keys (first-level keys) and do not compare their values.
This article will explain how to use array_diff_ukey() to compare the top-level keys of two multidimensional arrays and find out the different parts.
array_diff_ukey() is used to compare key names based on the user-provided callback function, so as to find out the parts in two or more arrays where the key names are not duplicated.
grammar:
array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array
$array1 : The array to be compared.
$array2 : The array used to compare with the first array.
$key_compare_func : A user-defined key comparison function.
Suppose you have two multidimensional arrays representing two different configuration sets. You want to find the top-level keys (first-level keys) that exist in the main configuration but not in the comparison configuration.
$configMain = [
'database' => [
'host' => 'localhost',
'port' => 3306
],
'cache' => [
'enabled' => true
],
'api' => [
'endpoint' => 'https://m66.net/api'
]
];
$configCompare = [
'database' => [
'host' => '127.0.0.1',
'port' => 3306
],
'cache' => [
'enabled' => false
]
];
We want to find out which top-level keys in $configMain do not appear in $configCompare (such as the api key in the above example).
<?php
function compareKeys($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? 1 : -1;
}
$configMain = [
'database' => [
'host' => 'localhost',
'port' => 3306
],
'cache' => [
'enabled' => true
],
'api' => [
'endpoint' => 'https://m66.net/api'
]
];
$configCompare = [
'database' => [
'host' => '127.0.0.1',
'port' => 3306
],
'cache' => [
'enabled' => false
]
];
$diffKeys = array_diff_ukey($configMain, $configCompare, 'compareKeys');
print_r($diffKeys);
Array
(
[api] => Array
(
[endpoint] => https://m66.net/api
)
)
As you can see, array_diff_ukey() successfully finds the top-level key api that exists in $configMain but not in $configCompare .
The callback function must be a function that compares two keys . The return value rules are the same as strcmp() : less than 0, equal to 0, and greater than 0.
This function does not compare values , only keys.
Only valid for top-level keys, if you want to compare nested structures, use recursive logic to match.
Using array_diff_ukey() is an efficient and concise way to compare key name differences between two arrays, especially for scenarios in multidimensional arrays where only primary structural differences are concerned.
Whether it is handling configuration differences, structure comparison, or performing permission verification, mastering the usage of this function can allow you to write cleaner and clearer PHP code.
If you want to further compare the differences between key-value pairs, you can consider using array_diff_assoc() or custom recursive function processing.