The array_udiff() is an inbuilt function in PHP and used for distinguishing between two or more array. The function compares the different values of two or more arrays by used of user-defined function data comparison and returns the differences. Actually, the function returns all the entries that are present in the first array which is not present in others array. If there are all values of all array are same then, return NULL array. Its unlike comparison of arr_diff()
array_udiff($array1, $array2, $array3..........array nth, arr_udiffFunction)
Parameters Used: This array_udiff() function parameters are described below:
- array1 :
It is the initial array and compares to another array…
- array2 :
The array Compared with the first array key.
Its Mandatory .
- array3… :
The second array Compared with the first array key.
- arr_udiffFunction: Its 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 types values which contain the first array which is present in all others array.If there are all of same then array return NULL array.
Note: This built-in function (array_udiff()) to compare the keys of an array and user-defined function to compare the values.
Example 1 :
Input : $arr1=array("a"=>"Geeksforgeeks", "b"=>"IDE", "d"=>"environment", "v"=>"Code Run"); $arr2=array("a"=>"Geeksforgeeks", "b"=>"Editor", "f"=>"Write", "h"=>"Code", ); Output: Array ( [b] => IDE [d] => environment [v] => Code Run ) Explanation: arr1 contains only three values(IDE, environment, Code Run) which is not present in arr2.
Example 2 :
Input: $arr1 = array("a"=>"Geeks", "x"=>"article", "n"=>"Geeks"); $arr2 = array("x"=>"Geeks", "y"=>"for", "z"=>"Geeks", "a"=>"IDE", "a"=>"Editor", ); Output: Array ( [x] => article ) Explanation: arr1 contains only one values(article) which is not present in arr2.
Let’s take a simple example to understand about array_udiff() Function.
Program 1: Take two array (array1 and array2) and using user-defined key comparison function (arr_udiffFunction).
Array ( [d] => Java [r] => XML )
Program: 2 Take four array (array1, array2, array3 and array4 ) and using user-defined key comparison function array_udiff().
Array ( [r] => ASP lab )
Program: 3 Take two array (array1 and array2) and using user-defined key comparison function (array_udiff(). which has match all element and values of array which is return NULL.
Array ( )