array_udiff_uassoc
Calculate the difference set of arrays with index checks, and use callback functions to compare data and indexes
The array_udiff_uassoc()
function is used to compare the key names and key values of two (or more) arrays and return the difference set.
Note: This function is compared using two user-defined functions; the first function compares the key names, and the second function compares the key values!
The function compares the key names and key values of two (or more) arrays and returns an array of differences that include all key names and key values in the array being compared ( array1 ) but not in any other parameter array ( array2 or array3 , etc.).
Compare the key names and key values of two arrays (compare with user-defined functions) and return the difference:
<?php function myfunction_key ( $a , $b ) { if ( $a === $b ) { return 0 ; } return ( $a > $b ) ? 1 : - 1 ; } function myfunction_value ( $a , $b ) { if ( $a === $b ) { return 0 ; } return ( $a > $b ) ? 1 : - 1 ; } $a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ; $a2 = array ( "a" => "red" , "b" => "green" , "c" => "green" ) ; $result = array_udiff_uassoc ( $a1 , $a2 , "myfunction_key" , "myfunction_value" ) ; print_r ( $result ) ; ?>
Try it yourself