Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | array_udiff_uassoc() Function

  • Last Updated : 07 Jul, 2021

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_Function, key_Function )

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_Function: This parameter represents the user-defined function. This user defined function will be used to compare values.
  3. key_Function: It is also a user-defined function. This user-defined function is used to compares arrays keys.

Note: Both comparison functions (value_Function, key_Function) 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: 

PHP




<?php
// PHP program to illustrate
// array_udiff_uassoc() function
 
// comparison function for array values
function value_Function($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
 
// comparison function for array keys
function key_Function($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_Function", "key_Function");
 
// print result.
print_r($result);
?>
Output: 
Array
(
    [n] => Java lab
    [o] => C# lab
    [y] => Ruby lab
)

 

Program: 2 

PHP




<?php
// PHP program to illustrate
// array_udiff_uassoc() function
 
// comparison function for array values
function value_Function($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
 
// comparison function for array keys
function key_Function($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_Function", "key_Function");
 
// print result.
print_r($result);
?>
Output: 
Array
(
    [e] => Ruby lab
)

 

Program: 3 

PHP




<?php
// PHP program to illustrate
// array_udiff_uassoc() function
 
// comparison function for array values
function value_Function($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
 
// comparison function for array keys
function key_Function($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_Function", "key_Function");
 
// print result.
print_r($result);
?>
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. 

PHP




<?php
// PHP program to illustrate
// array_udiff_uassoc() function
 
// comparison function for array values
function value_Function($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}
 
// comparison function for array keys
function key_Function($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_Function", "key_Function");
 
// print result.
print_r($result);
?>
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
Recommended Articles
Page :

Start Your Coding Journey Now!