PHP array_intersect_uassoc() Function

The array_intersect_uassoc() function compares the keys and values of two (or more) arrays, and returns the matches.

The comparison function returns an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

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

Parameter Description
array1 Required. The first array is the array that the others will be compared with
array2 Required. An array to be compared with the first array
array3,… Optional. An array to be compared with the first array
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_intersect_uassoc(array1,array2,array3...,myfunction) 

Example

function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

$array1=array("a"=>"Cooper","b"=>"Prince","c"=>"Samuel");
$array2=array("a"=>"Cooper","b"=>"Prince","d"=>"Samuel");
$array3=array("e"=>"James","a"=>"Cooper","d"=>"Samuel");

$result=array_intersect_uassoc($array1,$array2,$array3,"myfunction");
print_r($result);

Output

Array ( [a] => Cooper ) 

Leave a Reply

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