PHP array_intersect_ukey() Function

The array_intersect_ukey() function compares the keys of two (or more) arrays, and returns the matches.

The array_intersect_ukey() function compares the keys in two or more arrays, checking for matches, before comparing the keys in a user-defined function, then returns an array with the keys and values from the first array, if the function allows it.

Parameter Description
array1 Required. The first array is the array that the others will be compared with
array2 Required. An array to be compared with the first array
array3,… Optional. An array to be compared with the first array
myfunction Required. A string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument

Syntax

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

Example

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

$array1=array("a"=>"Cooper","b"=>"Prince","c"=>"Samuel");
$array2=array("a"=>"Cooper","b"=>"Prince","d"=>"Samuel");
$array3=array("e"=>"James","b"=>"Cooper","d"=>"Samuel");

$result=array_intersect_ukey($array1,$array2,$array3,"myfunction");
print_r($result);

Output

Array ( [b] => Prince )

Leave a Reply

Your email address will not be published. Required fields are marked *