PHP array_udiff_assoc() Function
Last Updated :
21 Aug, 2024
Improve
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 use of user-defined function with additional key and returns 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:
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 :
PHP
Output:
PHP
Output:
References: http://php.net/manual/en/function.array-udiff-assoc.php
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.
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).
<?php
// PHP code for array_udiff_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);
?>
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().
<?php
// PHP code for array_udiff_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);
?>
Array ( [b] => Page )
Related Article:
- 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.
References: http://php.net/manual/en/function.array-udiff-assoc.php