PHP array_intersect_ukey() function

Last Updated: February 12, 2022

array_intersect_ukey : Computes the intersection of arrays using keys for comparison. This function uses a callback function to do the comparison.

Syntax

 array_intersect_ukey(array $array, array ...$arrays, callable $key_compare_func): array

Supports (PHP 5, PHP 7, PHP 8)

Parameters

Parameter Description
array Required. array with values to check intersected values
arrays Required. arrays to be compared with the first array
key_compare_func Required. Callable function callback(mixed $a, mixed $b): int

Return Value

Return an array containing elements that are also appearing in other arrays

Example

<?php
function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

$array1 = array("1" => "one", "2" => "two", "3" => "three", "four");
$array2 = array("1" => "One", "3" => "three", "four", "five");
print_r(array_intersect_ukey($array1, $array2, "key_compare_func"));
?>

Output

Array ( [1] => one [3] => three [4] => four ) 
Test