This function takes two or more arrays as input and then returns the result array as intersection considering keys.
$new_array = array_intersect_ukey ($array1, $array2,....,my_function());
my_function() : is the callback function to return integer less than , equal to or greater than zero after comparing the keys.
Returns all elements of $array1 which are present in $array2 considering keys only.
Example with two arrays
function my_function($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$first=array('One' =>'First','Two'=>'Second','Three'=>'Third','Fourth');
$second=array('1st'=> 'First','Two'=>'2nd','Three'=>'3rd','Fourth');
$result=array_intersect_ukey($first,$second,"my_function");
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here.
Two -> Second
Three -> Third
0 -> Fourth
Keys of the first index are retained. ( no re-indexing done here) .
← Subscribe to our YouTube Channel here