PHP array_diff_uassoc() Function

The array_diff_uassoc() function is used to compare two or more arrays with an additional user supplied function. The function compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparison.

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_uassoc(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","c"=>"Samuel","d"=>"Cooper");
$array2=array("a"=>"Prince","b"=>"James","c"=>"Samuel");
$result=array_diff_uassoc($array1,$array2,"myfunction");
print_r($result);

Output

Array ( [d] => Cooper ) 

Leave a Reply

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