The array_intersect_ukey function simply compares the keys of two or more arrays using the user-defined function and returns the matches.
It takes the keys of the two arrays to be compared one by one and passes it to the user-defined function. The result contains an array that contains the elements whose keys are in the array 1 and in the other arrays.
What is the syntax of the array_intersect_ukey function in PHP?
array_intersect_ukey(array1, array2, array3, ..., myfunction)
Parameters | Details |
---|---|
array1 | The first array to compare with other arrays – required |
array2 | Second array to make a comparison against- required |
array3, ... | Further arrays to compare against – optional |
user-defined function | User-defined function to compare the keys of arrays. The function must return an integer <, = or > than 0 if the first argument is <, + or > than the second argument. |
Examples of array_intersect_ukey function
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>
In the above example, the user-defined function is used by the array_intersect_ukey function which receives the keys to compare. The result set contains the matching keys and their values in the two arrays.
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");
$result=array_intersect_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
?>
In the above example, we compare the keys of three arrays using the user-defined function.