The array_intersect_uassoc function compares keys and values of two or more arrays based on a user-defined key comparison function and returns the matching results. The user-defined function takes the keys only and passes them through the user-defined logic.
What is the syntax of the array_intersect_uassoc function in PHP?
array_intersect_uassoc(array1, array2, array3, ..., myfunction)
Parameters | Details |
---|---|
array1 | The first array to compare with other arrays – required |
array2 | Second array to make a comparison against- required |
array3, ... | Further arrays to compare against – optional |
user-defined function | User-defined function to compare the keys of arrays. The function must return an integer <, = or > than 0 if the first argument is <, + or > than the second argument. |
Examples of array_intersect_uassoc function
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
In the above example, the user-defined function implements a logic and returns 0, 1, or -1 based on the conditions. This function is used with the array_intersect_uassoc to compare the two arrays.
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_intersect_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
?>
In the above example, the user-defined function implements a logic and returns 0, 1, or -1 based on the conditions. This function is used with the array_intersect_uassoc to compare the three arrays.