PHP array_intersect_ukey() function
The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the entries from array1 which have matching keys that are present in all the other arrays.
Syntax
array_intersect_ukey(array $array1,array$array2[,array$...], callable$key_compare_func)
Parameter
array1(required)- This parameter signifies the input array to compare from.
array2(required)- This parameter signifies the input array to compare against.
array3….(optional)- It represents more arrays to compare against.
key_compare_func(required)- This parameter represents a string that describe a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Return
This function returns an array containing all the values in array1 which have matching keys present in all the other arrays.
Example 1
"green", "b" => "brown", "i" => "indigo", "red"); //initializing the input array1 $array2 = array("g" => "GREEN", "b" => "brown", "yellow", "red"); //compute the intersection of arrays using a callback function print_r(array_intersect_ukey($array1, $array2, "strcasecmp")); ?>
Output
Array ( [g] => green [b] => brown [0] => red )
Example 2
$b)?1:-1; } $array1=array("1"=>"red","2"=>"green","3"=>"blue"); $array2=array("1"=>"red","5"=>"green","9"=>"blue"); $array3=array("1"=>"red","4"=>"green","7"=>"red"); // compute the intersection of arrays using a callback function $intersection=array_intersect_ukey ($array1,$array2,$array3,"myfunction"); //returns the intersection print_r($intersection); ?>
Output
Array ( [1] => red )
Example 3
$b)? 1: 0; } // initializing the Input Array 1 $array1 = array(05=>"Reema", 10=>"raj", 30=>"Atul"); // initializing the Input Array 2 $array2 = array(05=>"Reema", 25=>"Reema", 30=>"Atul",70=>"Atul"); //compute the intersection of arrays using a callback function $intersection=array_intersect_ukey($array1, $array2, "my_function"); //returning the intersection print_r($intersection); ?>
Output
Array ( [5] =>Reema [10] => raj [30] => Atul )