Current Location: Home> Latest Articles> PHP array_intersect_ukey() Function Detailed Explanation and Usage Example

PHP array_intersect_ukey() Function Detailed Explanation and Usage Example

M66 2025-06-14

Detailed Explanation of PHP array_intersect_ukey() Function

The array_intersect_ukey() function in PHP compares the keys of multiple arrays using a user-defined comparison function. It returns an array containing the entries from the first array where the keys match in all other arrays.

Syntax

array_intersect_ukey(arr1, arr2, arr3, arr4, ..., compare_func)

Parameters

  • arr1 - Required. The first array to compare.
  • arr2 - Required. The second array to compare.
  • arr3 - Optional. Additional arrays to compare.
  • arr4 - Optional. Additional arrays to compare.
  • compare_func - Required. A user-defined comparison function used to compare the array keys. The function should return an integer: 0 if the keys are equal, 1 if the first key is greater than the second, and -1 otherwise.

Return Value

The array_intersect_ukey() function returns an array containing entries from the first array whose keys also exist in all the other arrays provided for comparison.

Example

Here is an example using a custom comparison function to compare the keys of arrays:


<?php
function check($a, $b) {
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

$arr1 = array("a" => "one", "b" => "two", "c" => "three");
$arr2 = array("a" => "one", "b" => "two");

$result = array_intersect_ukey($arr1, $arr2, "check");
print_r($result);
?>

Output


Array
(
    [a] => one
    [b] => two
)

This concludes the detailed explanation of the PHP array_intersect_ukey() function. You can now use this function for array key comparison based on your specific needs.