PHP Tutorial - PHP array_diff_ukey() function






Syntax

PHP array_diff_ukey() function has the following syntax.

array_diff_ukey(array1,array2,array3...,myfunction);

Definition

The array_diff_ukey() function compares the keys of two or more arrays with a user-defined function, and returns an array that contains the entries from array1 that are not present in array2 or array3, etc.

Parameter

ParameterIs RequiredDescription
array1Required.The array to compare from
array2Required.An array to compare against
array3,...Optional.More arrays to compare against
myfunctionRequired.A string that define a callable comparison function.

The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.





Example

Compare the keys of two arrays using a user-defined key comparison function, and return the differences:


<?php//from  ww w. j  av  a  2  s  .com
function myfunction($a,$b){
   if ($a===$b){
      return 0;
   }
   return ($a>$b)?1:-1;
}

$a1=array("a"=>"A","b"=>"B","c"=>"C","j"=>"java2s.com");
$a2=array("a"=>"A","b"=>"B","e"=>"E");

$result=array_diff_ukey($a1,$a2,"myfunction");
print_r($result);
?>

The code above generates the following result.