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

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function is used to get the difference between one or more given arrays using an user-defined function to compare the keys. It compares both the keys and the values between one or more arrays on the basis of user-defined function and returns the elements from the first array which are not present in the rest of arrays.

Syntax:

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

Parameter Values

Parameter Description
array1 This is a requiredparameter.This parameter defines the 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_diff_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"=>"1","b"=>"2","c"=>"3","d"=>"4");
$y=array("d"=>"1","b"=>"2","e"=>"3","d"=>"3");

$result=array_diff_uassoc($x,$y,"compare");  // compares both keys and values
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [c] => 3
    [d] => 4
)

Example 2:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("name"=>"Jerry","age"=>"21","place"=>"Noida","project"=>"WebApp");
$y=array("name"=>"Jerry","age"=>"19","place"=>"Noida","company"=>"Google");

$result=array_diff_uassoc($x,$y,"compare");  // compares both keys and values
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [age] => 21
    [project] => WebApp
)

No Sidebar ads