Open In App

PHP array_diff_uassoc() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 the arrays. The keys are compared according to the user-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_uassoc($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_function: 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 the 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

PHP




<?php
// PHP program to illustrate the  
// array_diff_uassoc() function 
  
// user defined function 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);
?>


Output:

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

Program 2

PHP




<?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);
?>


: Output:

Array
(
    [20] => raj
    [30] => geek
)
  • array_diff_uassoc() For Multidimensional Array:

array_diff_uassoc() function only works for one dimensional array. for checking multidimensional arrays with this function, we can use indexing method.

For Example, for 2D Array : 

array_diff_assoc($array1[index], $array2[index], $array3[index], ..., $arrayn[index], user_function)

In below Program, we are comparing two arrays which are positioned at first index of 2-dimensional arrays a1 & a2.

Program 3:

PHP




<?php
    function user_function($a, $b)
    {
      if ($a===$b)
      {
          return 0;
      }
      return ($a>$b)? 1: -1;
    }
    // Input Arrays
    $a1 = array(array("a" => "green", "b" => "yellow", "c" => "white", "black"), "b" => "brown", "c" => "blue", "red");
    $a2 = array(array("a" => "green", "b" => "yellow", "white", "d" => "black"), "yellow", "red");
  
    $result = array_diff_uassoc($a1[0], $a2[0], "user_function");
        
    print_r($result);
?>


Output:

Array
( 
     => white 
    [0] => black 
)

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads