Skip to content
Related Articles

Related Articles

PHP | array_udiff_uassoc() Function
  • Last Updated : 03 Jan, 2020

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:




<?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);
?>
Output:
Array
(
    [n] => Java lab
    [o] => C# lab
    [y] => Ruby lab
)

Program: 2




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

Array
(
    [e] => Ruby lab
)

Program: 3




<?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);
?>
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 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);
?>
Output:
Array
(
    [a] => C lab
    [b] => Java lab
    [d] => C# lab
)

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :