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.
array_udiff_assoc($array1, $array2, $array3……….array nth, arr_udiffassocFun)
Parameters Used: This array_udiff_assoc()function parameters are described below:
- array1 : It is the initial array and compares to another array. It is Mandatory.
- array2 : The array Compared with the first array key. It is Mandatory .
- array3 : The second array Compared with the first array key. It is Optional.
- 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.
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).
Array ( [b] => Ram [r] => David )
Program: 2 Takes four array (array1, array2, array3 and array4 ) and using user-defined key comparison function array_udiff_assoc().
Array ( [b] => Page )
- PHP | arr_diff() Function : Compute differences of an array.
- PHP | arr_udiff() Function: Compute differences of an array by used user-defined callback function and compare data.
- PHP | array_diff_assoc() Function : Compute differences of an array with additional index key.
- PHP | array_diff_uassoc() Function: 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 arrays.
- PHP | array_diff_key() Function: Compares the key of the first array of parameters with rest of the arrays and returns an array containing all the entries from $array1 that are not present in any of the other arrays.
Practice Similar Questions On:PHP
- How to get the function name inside a function in PHP ?
- D3.js | d3.set.has() Function
- p5.js | arc() Function
- p5.js | box() Function
- PHP | Ds\Set xor() Function
- p5.js | nfc() function
- D3.js | d3.set.add() Function
- p5.js | nfp() Function
- PHP | dir() Function
- PHP | exp() Function
- D3.js | d3.map.set() Function
- p5.js | nf() Function
- PHP | pi( ) Function
- PHP | Ds\Map map() Function
- PHP | Ds\Map last() Function
- PHP | end() Function
- PHP | Ds\Set first() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.