PHP | array_diff_uassoc() Function

The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array which are not there in the rest of arrays. The keys are compared according to the uder defined function supplied to this function.

Note: This function is different than PHP | array_diff_assoc() Function as in array_diff_assoc() function the keys are compared according to some internal function whereas in array_diff_uassoc() function the keys are compared according to a user defined function passed to it as parameter.

Syntax:



array_diff_assoc($array1, $array2, $array3, ..., $arrayn, user_function)

Parameters: This function accepts a list of arrays as parameter and a user defined function which will be used for key’s comparison.

  1. list_of_array: This function takes a list of arrays separated by spaces from which we want to find the difference. In the above syntax the list of array is $array1, $array2, $array3, …, $arrayn. This list must contain atleast two arrays otherwise a warning will be thrown.
  2. user_fucntion: This is a string type parameter which represents the name of the user defined function which will be used for key’s comparison. The function returns either of an integer which is smaller, greater or equal to 0 if the first argument is greater, smaller or equal than the second argument.

Return Value: This function returns an array containing the elements of the first array $array1 which are not present in the other arrays passed to it in parameters. The comparison is done between key and value of the first array $array1 with rest of the arrays. The comparison of keys is done as per the user-defined function.

Examples:

Input : $a1=array(10=>"striver", 20=>"raj", 30=>"geek")
        $a2=array(20=>"striver", 10=>"raj", 30=>"geek")
        function user_function($a, $b)
        {
           if ($a===$b)
           {
              return 0;
           }
           return ($a>$b)?1:-1;
        }

Output: Array
        (
           [10] => striver
           [20] => raj
        )

Explanation:  Since user_function returns 0 when keys
are equal and 1 and -1 when greater and less respectively.
So, the elements with unequal keys are in the output array.

Below programs illustrate the array_diff_uassoc() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the  
// array_diff_uassoc() function 
  
// user defined fucntion that returns 0 if 
// $array1's keys are equal to any other 
// input array, else returns 1 if greater, 
// or -1 if smaller 
function user_function($a, $b)
{
  if ($a===$b)
  {
      return 0;
  }
  return ($a>$b)? 1: -1;
}
  
// Input Arrays
$a1=array(10=>"striver", 20=>"raj", 30=>"geek");
$a2=array(20=>"striver", 10=>"raj", 30=>"geek");
  
$result = array_diff_uassoc($a1, $a2, "user_function");
  
print_r($result);
?>

chevron_right


Output:

Array
(
    [10] => striver
    [20] => raj
)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate the 
// array_diff_uassoc() function 
  
// user defined function that returns 1 if 
// $array1's keys are equal to any other 
// input array, else returns 1 if greater, 
// or 0 if smaller 
function user_function($a, $b)
{
  if ($a===$b)
  {
    return 1;
  }
  return ($a>$b)? 1: 0;
}
  
// Input Arrays
$a1 = array(10=>"striver", 20=>"raj", 30=>"geek");
$a2 = array(20=>"striver", 10=>"raj", 30=>"geek");
  
$result=array_diff_uassoc($a1, $a2, "user_function");
print_r($result);
?>

chevron_right


:
Output:

Array
(
    [20] => raj
    [30] => geek
)

Reference:
http://php.net/manual/en/function.array-diff-uassoc.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.