In this tutorial, you shall learn about PHP array_diff_uassoc() function which can find the difference of an array against other arrays based on user defined callback function, with syntax and examples.
PHP array_diff_uassoc() Function
PHP array_diff_uassoc() function computes the difference of an array against other arrays. The logic for comparison is done using user defined callback function.
In this tutorial, we will learn the syntax of array_diff_uassoc(), and how to use this function to find the difference of an array from other arrays, covering different scenarios based on array type and arguments.
Syntax of array_diff_uassoc()
The syntax of PHP array_diff_uassoc() function is
array_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_compare_func ) : array
where
Parameter | Description |
---|---|
array1 | [mandatory] Array of interest. Compare the items of this array against other arrays’. |
array2 | [mandatory] Array of reference. The items of this array are used to comparison against. |
… | [optional] More arrays, along with array2, to compare against. |
key_compare_func | This function must return an integer: 1. less than zero, if first argument is less than second argument. 2. equal to zero, if first argument equals second argument. 3. greater than zero, if first argument is greater than second argument. |
Return Value
The array_diff_uassoc() function returns an array whose key-value pairs are present in $array1 but not present in $array2 or other arrays(if provided).
Examples
1. Compute difference of arrays based on a user defined function
In this example, we will take an associative array, array1, with key-value pairs, and find the difference of it against another array, array2, using array_diff_uassoc() function.
PHP Program
<?php
function key_compare_func($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');
$array2 = array('a'=>'apricot', 'b'=>'banana', 'c'=>'cherry');
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>
Output
'a'=>'apple'
and 'a'=>'apricot'
differ, and hence become a part of the result. Other items of array1 are present in array2.
2. Compute difference of Array against Multiple Arrays
In this example, we will take an array: array1 and find the difference of this array against two other arrays: array2 and array3, using array_diff_uassoc() function.
PHP Program
<?php
function key_compare_func($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');
$array2 = array('a'=>'apple');
$array3 = array('c'=>'cherry');
$result = array_diff_uassoc($array1, $array2, $array3, "key_compare_func");
print_r($result);
?>
Output
'a'=>'apple'
is present in array2, and 'c'=>'cherry'
is present in array3. Rest of the key-value pairs of array1 are not present in any of the arrays [array2, array3]. So, those key-value pairs of array1, that are not present in other arrays are returned in the resulting array.
Conclusion
In this PHP Tutorial, we learned how to compute the difference of arrays considering key/index along with value, using PHP Array array_diff_uassoc() function.