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

PHP array_uintersect() Function

PHP array_uintersect() Function

PHP array_uintersect() function is used to calculate the intersection of two or more given arrays depending on the values. It compares the first array values with all the other arrays using an user-defined function and returns the matches.

Syntax:

array_uintersect(array1, array2, array3, ..., function);

Parameter Values

Parameter Description
array1 This is a requiredparameter.This parameter defines array to compare from.
array2 This is a requiredparameter.This parameter definesan array to compare against.
array3,... This is an optionalparameter.This parameter definesmore arrays to compare against.
function This is a requiredparameter.This parameter definesa string that define a callable comparison function.

Here is an example of array_uintersect() function in PHP:

<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("e"=>"1","f"=>"2","g"=>"4");
$result=array_uintersect($x,$y,"compare");
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [d] => 4
)

Example 2:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("1","2","3","4","5","6","7","8","9");
$y=array("1","2","4","7","11");
$result=array_uintersect($x,$y,"compare");
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [6] => 7
)

No Sidebar ads