Home >Backend Development >PHP Tutorial >php-Arrays function-array_intersect_ukey-use callback function to compare key names to calculate the intersection of arrays_PHP tutorial
array_intersect_ukey() uses callback function to compare key names to calculate the intersection of arrays
【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.
This comparison is performed via 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_intersect_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.../optional/provides users with a callback function as a comparison standard
【Example】
[php]
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);
var_dump(array_intersect_ukey( $array1,$array2,'key_compare_func'));
/*
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}
*/
Excerpted from zuodefeng’s notes