Home >>PHP Array Functions >PHP array_intersect_ukey() Function
PHP array_intersect_ukey() Function is used to calculate the intersection of two or more given input array against keys with the help of user-defined function. It accepts two or more arrays and a $Function as parameter. It returns an array as output containing the value of the first array whose keys exist in all others arguments.
Syntax:
array_intersect_ukey($array1, $array2, $array3, ..., function);
Parameter | Description |
---|---|
array1 | This is a required parameter. This parameterdefines the array that the others will be compared with. |
array2 | This is a required parameter. This parameterdefinesan array to be compared with the first array. |
array3,... | This is an optional parameter. This parameterdefinesmore array to be compared with the first array. |
Myfunction | This is a required parameter. This parameterdefines a callable comparison function. |
Here is an example of array_intersect_ukey() function in PHP:
<html> <body> <pre> <?php function compare($x,$y) { if ($x===$y) { return 0; } return ($x>$y)?1:-1; } $x=array("a"=>"Abhi","b"=>"Jerry","c"=>"Noida","d"=>"Python"); $y=array("a"=>"Abhi","c"=>"Noida","d"=>"PHP"); $result=array_intersect_ukey($x,$y,"compare"); print_r($result); ?> </pre> </body> </html>
Array ( [a] => Abhi [c] => Noida [d] => Python )
Example 2:
<html> <body> <pre> <?php function compare($x,$y) { if ($x===$y) { return 0; } return ($x>$y)?1:-1; } $x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5"); $y=array("a"=>"1","b"=>"2","c"=>"3","d"=>"5","e"=>6); $result=array_intersect_ukey($x,$y,"compare"); print_r($result); ?> </pre> </body> </html>
Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 [e] => 5 )