Home >>PHP Array Functions >PHP array_intersect_uassoc() Function

PHP array_intersect_uassoc() Function

PHP array_intersect_uassoc() Function

PHP array_intersect_uassoc() Function is used to compare key and values of two or more given input arrays by using a user-defined comparison function and return the matches. It accepts minimum three parameters and all three parametersare mandatory and the others parameters are optional. It returns an array containing the elementsof array1 that are present in all other arrays.

Syntax:

array_intersect_uassoc($array1, $array2, $array3, ..., function);

Parameter Values

Parameter Description
array1 This is a required parameter. This parameterdefines the array that the others will be compared with.
array2 This is a required parameter. This parameterdefinesan array to be compared with the first array.
array3,... This is an optional parameter. This parameterdefinesmore array to be compared with the first array.
Myfunction This is a required parameter. This parameterdefines a callable comparison function.

Here is an example ofarray_intersect_uassoc() function in PHP:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("a"=>"Abhi","b"=>"Jerry","c"=>"Noida","d"=>"Python");
$y=array("a"=>"Abhi","c"=>"Noida","d"=>"PHP");
$result=array_intersect_uassoc($x,$y,"compare");
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => Abhi
    [c] => Noida
)

Example 2:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5");
$y=array("a"=>"1","b"=>"2","c"=>"3","d"=>"5","e"=>6);
$result=array_intersect_uassoc($x,$y,"compare");
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

No Sidebar ads