Current Location: Home> Latest Articles> In-depth Guide to PHP’s array_intersect_ukey() Function and Its Usage

In-depth Guide to PHP’s array_intersect_ukey() Function and Its Usage

gitbox 2025-06-15

1. Introduction

PHP is a widely used programming language that comes with a rich set of built-in functions to simplify various development tasks. Arrays are one of the most commonly used data structures in PHP, and PHP provides numerous built-in functions for array manipulation. In this article, we focus on the array_intersect_ukey() function, which is specifically designed to compare array keys, helping you understand and utilize it effectively.

2. Definition of array_intersect_ukey()

The array_intersect_ukey() function compares the keys of two or more arrays and returns the keys that exist in all arrays. It supports multiple arrays as input and allows a custom callback function to control how keys are compared.

3. Syntax

array_intersect_ukey(array1, array2, array3..., callback_function)

3.1 Parameters

array1: Required. The first array to compare.

array2: Required. The second array to compare.

array3,...: Optional. Additional arrays to compare.

callback_function: Optional. A user-defined callback function to compare the keys. If omitted, PHP’s default key comparison is used.

3.2 Return Value

Returns an array containing all elements whose keys are present in all input arrays. If there are no common keys, it returns an empty array.

3.3 About the Callback Function

The callback function receives two keys as arguments and must return an integer: return 0 if the keys are considered equal, or a non-zero value if they differ. This enables flexible key comparison rules such as case-insensitive matching.

Example callback function:

function myfunction($key1, $key2) {
    if ($key1 == $key2)
        return 0;
    elseif ($key1 > $key2)
        return 1;
    else
        return -1;
}

4. Usage Example

The example below demonstrates how to use array_intersect_ukey() to compare keys of two arrays, employing PHP’s built-in strcasecmp() function for case-insensitive key comparison:

$first_array = array('a' => 'red', 'b' => 'green', 'c' => 'blue');
$second_array = array('a' => 'apple', 'b' => 'banana', 'd' => 'orange');
$result = array_intersect_ukey($first_array, $second_array, 'strcasecmp');
print_r($result);

Output:

Array
(
    [a] => red
    [b] => green
)

Here, the function matches keys ‘a’ and ‘b’ using strcasecmp(), returning their associated elements.

5. Important Notes

1. array_intersect_ukey() compares only the keys, not the values.

2. Key types should be consistent (e.g., integer vs. string) to avoid mismatches.

3. Keep the callback function lightweight to maintain performance when working with large arrays.

6. Conclusion

This article explained the features, syntax, and practical usage of PHP’s array_intersect_ukey() function. By comparing keys across multiple arrays with customizable comparison logic, this function simplifies key-based data filtering and enhances code maintainability and efficiency.