PHP | array_udiff_assoc() Function

The array_udiff_assoc() is an inbuilt function in PHP and used for distinguishing between two or more arrays. The function computes the difference arrays from two or more arrays by used of user-defined function with additional key and return differences. It returns all the entries that are present in the first array and not present in any other array. It is different from array_diff_assoc() function as it allows a user defined function to decide the criteria.

Syntax:
array_udiff_assoc($array1, $array2, $array3……….array nth, arr_udiffassocFun)

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

  1. array1 : It is the initial array and compares to another array. It is Mandatory.
  2. array2 : The array Compared with the first array key. It is Mandatory .
  3. array3 : The second array Compared with the first array key. It is Optional.
  4. arr_udiffassocFun: It’s required user-defined function and A string that define user-defined callback function and it return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Return Value:
Returns an array which contains elements of first array which are not present in all others arrays. If there are all array element present then array return NULL.

Note: This built-in function array_udiff_assoc()) to compare the keys of an array and user-defined function to compare the values. The function checks only one dimension of a n-dimensional array.



Example 1 :


Input :
 $arr1 = array(
    "a" => "Geeks",
    "b" => "for",
    "d" => "geeks"
);
$arr2 = array(
    "a" => "Geeks",
    "y" => "is",
    "d" => "geeks"
);
Output:
Array
(
    [b] => for
)
Explanation: arr1 contains only one 
values(for) which is not present in 
arr2.

Example 2 :

Input:
$arr1 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "Java",
    "x" => "XML",
    "y" => "C#"
);
$arr2 = array(
    "a" => "C",
    "b" => "C++",
    "d" => "PHP",
    "x" => "Advanced PHP",
    "n" => "XML"    
);
Output:
Array
(
    [d] => Java
    [x] => XML
    [y] => C#
)

Explanation: arr1  return three values
(article) which are not present in arr2.
But XML is present both arr1 and arr2 
but both have different key.

Let’s take a simple example to understand about array_udiff_assoc() Function.

Program 1: Take two array (array1 and array2) and using user-defined key comparison function (arr_udiffassocFun).

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
<?php
// PHP code for array_uintersect_assoc function.
  
// This function is used to decide which elements
// to pick array_udiff_assoc
function arr_udiffassocFun($x, $y)
{       
    return ($x === $y)? 0 : 1;
}
  
// array list for comparison.
$arr1 = array(
    "a" => "Raj",
    "b" => "Ram",
    "d" => "Denish",
    "r" => "David"
);
$arr2 = array(
    "a" => "Raj",
    "y" => "Ram",
    "d" => "Denish",
    "x" => "Ritche"
);
  
// Driver code
$result = array_udiff_assoc($arr1,
             $arr2, "arr_udiffassocFun");
print_r($result);
?>

chevron_right


Output:

Array
(
    [b] => Ram
    [r] => David
)

Note : [b] => Ram is printed because key is different. In array_diff_assoc() and array_udiff_assoc(), keys and values both are compared. array_diff() compares only values.

Program: 2 Takes four array (array1, array2, array3 and array4 ) and using user-defined key comparison function array_udiff_assoc().

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code for array_uintersect_assoc function
// This function is used to decide which elements
// to pick array_udiff_assoc
function arr_udiffassocFun($x, $y)
{       
    return ($x === $y)? 0 : 1;
}
  
// array list for comparison.
$arr1 = array(
    "a" => "Larry",
    "b" => "Page",
    "d" => "Denish",
    "r" => "Ritche"
);
$arr2 = array(
    "a" => "Larry",
    "y" => "Page",
    "d" => "Denish",
    "r" => "Ritche"
);
$arr3 = array(
    "a" => "larry",
    "y" => "Bill Gate",
    "d" => "Denish",
    "r" => "Willion"
);
$arr4 = array(
    "a" => "Raj",
    "y" => "Bill Gate",
    "d" => "Denish",
    "r" => "Woks"
);
$result = array_udiff_assoc($arr1,
   $arr2, $arr3, $arr4, "arr_udiffassocFun");
// print result.
print_r($result);
?>

chevron_right


Output:

Array
(
    [b] => Page
)

Related Article:

Practice Similar Questions On:PHP

References: http://php.net/manual/en/function.array-udiff-assoc.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.


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.