The array_intersect_ukey()
function is used to compare the key names of two (or more) arrays and return the intersection.
Note: This function uses a user-defined function to compare key names!
The function compares the key names of two (or more) arrays and returns an intersection array that includes all key names in the array being compared ( array1 ) and in any other parameter array ( array2 or array3 , etc.).
Compare the key names of two arrays (using user-defined functions to compare key names) and return the intersection:
<?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 ) ; ?>
Try it yourself
Compare the key names of three arrays (using user-defined functions to compare key names) and return the intersection:
<?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 ) ; ?>
Try it yourself
array_intersect_ukey ( array1 , array2 , array3 ... , myfunction )
parameter | describe |
---|---|
array1 | Required. The first array that is compared with other arrays. |
array2 | Required. The array that compares to the first array. |
array3 ,... | Optional. Other arrays that are compared with the first array. |
myfunction | Required. Defines a string that calls the comparison function. If the first parameter is less than, equal to or greater than the second parameter, the comparison function must return an integer less than, equal to or greater than 0. |
The array_intersect_ukey()
function uses the callback function to compare key names to calculate the intersection of an array.
array_intersect_ukey()
returns an array containing all the key names that appear in array1 and appear in all other parameter arrays at the same time.
This comparison is done through a user-provided callback function. This function takes two parameters, namely the two key names to be compared. If the first parameter is smaller than the second parameter, the function returns a negative number, if the two parameters are equal, it returns 0, and if the first parameter is larger than the second parameter, it returns a positive number.