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

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() function is used to get the difference between two or more given arrays. It compares two or more arrays according to their keys and values using user-defined function and returns the elements that are present in the first array but not in the other input arrays.

Syntax:

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

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","f"=>"6");
$y=array("b"=>"3","c"=>"4","e"=>"4","f"=>"6");
$result=array_udiff_assoc($x,$y,"compare");  // keys and value both should match
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [b] => 2
    [c] => 3
    [d] => 4
    [e] => 5
)

No Sidebar ads