Home > Article > Backend Development > php-Arrays function-array_diff_ukey-use callback function to compare key names to calculate the difference set of array_PHP tutorial
array_diff_ukey() function uses the callback function to compare the key names and calculate the difference set of the array
【Function】
This function will return an array,
This array contains the values of all keys that are in array1 but are not in any other parameter array.
Note that the relationship remains unchanged. This comparison is performed through a user-supplied callback function.
If the first parameter is considered to be less than, equal to, or greater than the second parameter,
must be returned
An integer less than zero, equal to zero, or greater than zero
【Scope of use】
php5 > 5.1.0
【Use】
array array_diff_ukey( array array1, array array2[,array...,callback key_compare_func] )
array1/required/array1
array2/required/comparable array must have at least one
array.../optional/array used for comparison
key_compare_func.../Required/Provide users with a callback function as a comparison standard
【Example】
[php]
//Define callback function
function key_compare_func( $key1, $key2 )
{
If( $key1 == $key2 )
Return 0;
else if( $key1 > $key2 )
Return 1;
else
Return -1;
}
//Define two arrays respectively
$array1 = array( "blue" => 1, "red" => 2, "green" => 3, "purple" => 4 );
$array2 = array( "green" => 5, "blue" => 6, "yellow" => 7, "cyan" => 8 );
print_r( array_diff_ukey( $array1, $array2, "key_compare_func" ) );
/*
Array
(
[red] => 2
[purple] => 4
)*/
Excerpted from zuodefeng’s notes