array_diff_ukey() Function in PHP

The array_diff_ukey() function in PHP is a part of the Array functions. It is used to compute the difference between two or more arrays based on the values of the keys.

array_diff_ukey() returns an array containing all the entries from the first array that are not present in any of the subsequent arrays. The array keys are preserved.

Version Support: array_diff_ukey() in PHP 8.5, array_diff_ukey() in PHP 8.4, array_diff_ukey() in PHP 8.3, array_diff_ukey() in PHP 8.2, array_diff_ukey() in PHP 8.1, array_diff_ukey() in PHP 7.1, array_diff_ukey() in PHP 7.2, array_diff_ukey() in PHP 7.3, array_diff_ukey() in PHP 7.4

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

array_diff_ukey($array1, $array2, $key_function)

Here, $array1 and $array2 are the input arrays, and $key_function is a function that takes a key from $array1 and returns a value to be compared.

Example:

$array1 = array( a => 1, b => 2, c => 3);
$array2 = array( b => 2, c => 3, d => 4);

function key_func($key) {
   return $key;
}

$result = array_diff_ukey($array1, $array2, key_func );
print_r($result);

In this example, the output will be:

Array
(
   [a] => 1
)

This means that the key a is present in $array1 but not in $array2.

22 Views