Current Location: Home> Latest Articles> Use array_diff_ukey() to find out the difference between the key names of two arrays

Use array_diff_ukey() to find out the difference between the key names of two arrays

M66 2025-05-15

In PHP, array_diff_ukey() is a very useful function that can be used to compare key names of two arrays and return those key names in the first array that are not in the second array. This function is very suitable for us when we need to find out the differences in the key names of two arrays, especially when the key names are more complicated, we can judge them through a custom comparison function.

Function introduction

The prototype of the array_diff_ukey() function is as follows:

 array array_diff_ukey ( array $array1 , array $array2 , callable $key_compare_func )
  • $array1 : The first array to be compared.

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

  • $key_compare_func : A custom comparison function for comparing two key names. This function takes two key names as parameters and should return an integer representing the result of the comparison.

Sample code

Let's look at a specific example to demonstrate how to use the array_diff_ukey() function to find the difference in key names between two arrays.

 <?php
// Define two arrays
$array1 = [
    'apple' => 1,
    'banana' => 2,
    'cherry' => 3,
];

$array2 = [
    'banana' => 4,
    'date' => 5,
    'fig' => 6,
];

// Custom comparison functions
function key_compare_func($key1, $key2) {
    // Directly compare whether the key names are equal
    return strcmp($key1, $key2);
}

// use array_diff_ukey() Comparison of key names of two arrays
$result = array_diff_ukey($array1, $array2, 'key_compare_func');

// Output result
print_r($result);
?>

Results output:

 Array
(
    [apple] => 1
    [cherry] => 3
)

Analysis

In the example above, we have two arrays $array1 and $array2 , where $array1 contains key names 'apple' , 'banana' and 'cherry' , and $array2 contains key names 'banana' , 'date' and 'fig' . By calling array_diff_ukey() , we pass in a custom comparison function key_compare_func , which is used to compare whether the two key names are the same. Finally, the function returns a new array containing the key names 'apple' and 'cherry' in $array1 , because these two key names do not appear in $array2 .

Use custom comparison functions

The power of array_diff_ukey() is that it supports custom comparison functions. This means that we can not only perform simple key name comparisons, but also write complex comparison logic according to our own needs. For example, if we need to ignore case differences in key names, we can do this in the comparison function.

Example: Ignore case comparison

 <?php
// Define two arrays
$array1 = [
    'Apple' => 1,
    'banana' => 2,
    'cherry' => 3,
];

$array2 = [
    'apple' => 4,
    'Date' => 5,
    'fig' => 6,
];

// Custom comparison functions,Ignore case
function case_insensitive_key_compare_func($key1, $key2) {
    return strcasecmp($key1, $key2);
}

// use array_diff_ukey() Comparison of key names of two arrays
$result = array_diff_ukey($array1, $array2, 'case_insensitive_key_compare_func');

// Output result
print_r($result);
?>

Results output:

 Array
(
    [banana] => 2
    [cherry] => 3
)

In this example, although the 'Apple' in $array1 and 'apple' key names in $array2 are cased differently, since we use the strcasecmp function for comparison, PHP ignores the case differences and considers them the same key names. Therefore, the final result will only return the two key names 'banana' and 'cherry' , because 'apple' is considered to have been found in $array2 .

Summarize

By using the array_diff_ukey() function, we can easily find the difference in key names between two arrays. This function is very powerful and supports custom comparison logic, so it can cope with various complex comparison needs. Whether it is a simple key name comparison or a comparison that requires ignoring upper and lower case, array_diff_ukey() can be easily handled.