PHP array_diff_ukey() Function

The array_diff_ukey() function is used to compare two or more arrays using an additional user supplied function on the keys for comparison.The function compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Note: This function uses a user-defined function to compare the keys!

Parameter Description
array1 Required. The array to compare from
array2 Required. An array to compare against
array3,… Optional. More arrays to compare against
myfunction Required. 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

Syntax

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

Example

function myfunction($a,$b)
{
	if ($a==$b)
	{
	  return 0;
	}
	return ($a>$b)?1:-1;
}
$array1=array("a"=>"Prince","b"=>"James","d"=>"Samuel","c"=>"Cooper");
$array2=array("a"=>"Prince","b"=>"James","c"=>"Samuel");
$result=array_diff_ukey($array1,$array2,"myfunction");
print_r($result);

Output

Array ( [d] => Samuel ) 

Leave a Reply

Your email address will not be published. Required fields are marked *