array_uintersect
Calculate the intersection of the array and compare the data using the callback function
The array_uintersect()
function is used to compare the key values of two (or more) arrays and return the intersection.
Note: This function uses a user-defined function to compare key values.
The function compares the key values of two (or more) arrays and returns an intersection array that includes all key values in the array being compared ( array1 ) and also in any other parameter array ( array2 or array3 , etc.).
Compare the key values of two arrays (using user-defined functions to compare key values) 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_uintersect ( $a1 , $a2 , "myfunction" ) ; print_r ( $result ) ; ?>
Try it yourself
Compare the key values of three arrays (using user-defined functions to compare key values) 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" , "yellow" ) ; $a2 = array ( "A" => "red" , "b" => "GREEN" , "yellow" , "black" ) ; $a3 = array ( "a" => "green" , "b" => "red" , "yellow" , "black" ) ; $result = array_uintersect ( $a1 , $a2 , $a3 , "myfunction" ) ; print_r ( $result ) ; ?>
Try it yourself