PHP Function Reference

PHP array_intersect_ukey() Function



The PHP array_intersect_ukey() function compares keys of an array against one or more other arrays and returns all elements of the first array that are present in all the arguments. This function compares keys using a user-defined comparison function.

Unlike array_intersect(), this function compares keys instead of the values. Unlike array_intersect_key(), this function uses a user-defined function to compare keys.

Syntax

array_intersect_ukey(array, arrays, myfunction)

Parameters

array Required. Specify an array to compare from.
arrays Required. Specify one or more arrays to compare against.
myfunction Required. Specify user-defined function to compare keys. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return Value

Returns the values of array whose keys exist in all the arguments.

Exceptions

NA.

Example:

The example below shows the usage of array_intersect_ukey() function.

<?php
function MyFunc($x, $y) {
  if ($x == $y) 
    return 0;
  return ($x < $y)? -1: 1;
}

$Arr1 = array("a"=>"Red",
              "b"=>"Red",
              "c"=>"Blue",
              "d"=>"Blue",
              "e"=>"Green");
$Arr2 = array("a"=>"Red", 
              "b"=>"Blue", 
              "c"=>"Green");

//comparing Arr1 against Arr2
$Arr1_intersect_Arr2 = 
    array_intersect_ukey($Arr1, $Arr2, "MyFunc");

//displaying the result
print_r($Arr1_intersect_Arr2);

The output of the above code will be:

Array
(
    [a] => Red
    [b] => Red
    [c] => Blue
)

❮ PHP Array Reference