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

PHP array_udiff() Function

PHP array_udiff() Function

PHP array_udiff() function is used to difference between two or more given array. It compares the different values of two or more arrays by using the user-defined function and returns the differences.

Syntax:

array_udiff(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 anoptionalparameter.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_udiff() function in PHP:

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

Example 2:

<html>
<body>
<?php
function compare($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5","f"=>"6");
$y=array("e"=>"1","f"=>"2","g"=>"3");
$z=array("h"=>"3","i"=>"4","k"=>"5","l"=>"6","m"=>"7");
$result=array_udiff($x,$y,$z,"compare");  // it will return a empty array
print_r($result);
?>
</body>
</html>
Output:
Array ()

No Sidebar ads