PHP | array_udiff_uassoc() Function

The array_udiff_uassoc() Function is an inbuilt function in PHP and is used for distinguishing between two or more array. The function computes the differences array by use of two user-defined functions with the additional index key. It compares both data and index by use of callback functions and returns the difference.

Syntax :

array_udiff_uassoc($arr1, $arr2, $arr3........nth array, value_Funtion, key_Funtion )

Parameters Used: This array_udiff_uassoc() function parameters are described below:

  1. $arr1, $arr2, $arr3, $arr4,…., $arrn : This represents the list of arrays which we want to distinguish.
  2. value_Funtion: This parameter repesents the user-defined function. This user defined function will be used to compare values.
  3. key_Funtion: It is also a user-defined function. This user-defined function is used to compares arrays keys.

Note: Both comparison functions (value_Funtion, key_Funtion) returns an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return Value: This function returns an array containing all values of the array $arr1 which are not present in any other arrays.



Some related functions:

Below programs illustrate the array_udiff_uassoc() Function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// array_udiff_uassoc() function
  
// comparison function for array values
function value_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// comparison function for array keys
function key_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
  
// array1  list for comparison.
$arr1 = array(
    "m" => "C lab",
    "n" => "Java lab",
    "o" => "C# lab",
    "x" => "C++ lab",
    "y" => "Ruby lab",
);
  
//array2  list for comparison.
$arr2 = array(
       "m" => "C lab",
    "b" => "Java lab",
    "c" => "C# lab",
    "x" => "C++ lab",
    "n" => "Ruby lab",
);
  
$result = array_udiff_uassoc($arr1,
    $arr2, "value_Funtion", "key_Funtion");
  
// print result.
print_r($result);
?>

chevron_right


Output:

Array
(
    [n] => Java lab
    [o] => C# lab
    [y] => Ruby lab
)

Program: 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// array_udiff_uassoc() function
  
// comparison function for array values
function value_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// comparison function for array keys
function key_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
  
// array1  list for comparison.
$arr1 = array(
    "a" => "C lab",
    "b" => "Java lab",
    "c" => "C# lab",
    "d" => "C++ lab",
    "e" => "Ruby lab",
);
  
// array2  list for comparison.
$arr2 = array(
       "a" => "C lab",
    "b" => "Java lab",
    "c" => "C# lab",
    "d" => "C++ lab",
    "e" => "XML lab",
);
$arr3 = array(
    "a" => "C lab",
    "b" => "Java lab",
    "c" => "C# lab",
    "d" => "C++ lab",
    "e" => "CSS lab"
);
$arr4 = array(
    "a" => "C lab",
    "b" => "Java lab",
    "c" => "C# lab",
    "d" => "C++ lab",
    "e" => "PHP lab"
);
  
$result = array_udiff_uassoc($arr1, $arr2
    $arr3, $arr4, "value_Funtion", "key_Funtion");
  
// print result.
print_r($result);
?>

chevron_right


Output:


Array
(
    [e] => Ruby lab
)

Program: 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// array_udiff_uassoc() function
  
// comparison function for array values
function value_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// comparison function for array keys
function key_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
  
// array1  list for comparison.
$arr1 = array(
    "x" => "Geeks",
    "y" => "for",
    "z" => "Geeks",
);
  
// array2  list for comparison.
$arr2 = array(
     "x" => "Geeks",
    "y" => "for",
    "z" => "Geeks",
);
  
  
$result = array_udiff_uassoc($arr1,
    $arr2, "value_Funtion", "key_Funtion");
  
// print result.
print_r($result);
?>

chevron_right


Output:

Array
(
)

Program: 4 Take three array (array1 and array2, array3) and using comparison function array_udiff_uassoc() Function. Where all three array has same index but values are different then return first array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to illustrate
// array_udiff_uassoc() function
  
// comparison function for array values
function value_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
// comparison function for array keys
function key_Funtion($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
  
  
// array1  list for comparison.
$arr1 = array(
    "a" => "C lab",
    "b" => "Java lab",
    "d" => "C# lab",
);
  
// array2  list for comparison.
$arr2 = array(
       "a" => "C ",
    "b" => "Java ",
    "d" => "C#",
);
  
// array3  list for comparison.
$arr3 = array(
    "a" => "Program",
    "b" => "Code",
    "d" => "Run",
  
);
  
$result = array_udiff_uassoc($arr1,
   $arr2, $arr3, "value_Funtion", "key_Funtion");
  
// print result.
print_r($result);
?>

chevron_right


Output:

Array
(
    [a] => C lab
    [b] => Java lab
    [d] => C# lab
)

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




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Akanksha_Rai

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.