array_udiff
Use callback function to compare data to calculate the difference set of arrays
array_udiff()
function is used to compare the key values of two (or more) arrays and return the difference set.
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 array of differences that include all key values in the array being compared ( array1 ) but not 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 difference:
<?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_udiff ( $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 difference:
<?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_udiff ( $a1 , $a2 , $a3 , "myfunction" ) ; print_r ( $result ) ; ?>
Try it yourself