w3resource

PHP: array_diff_uassoc() function

PHP: Compares an array against another and returns the difference

The array_diff_uassoc() function is used to compare two or more arrays with an additional user supplied function. The function compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparison.

Version:

(PHP 4 and above)

Syntax:

array_diff_uassoc(array1, array2, array3,.....user-defined-function)

Parameter:

Name Description Required /
Optional
Type
array1 The specified array which will be compared with other arrays. Required Array
array2 Compared with the first array. Required Array
array3 Compared with the first array. Optional Array
user-defined-function The specified callback function. The function returns an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. Required -

Return value:

An array containing all the entries from array1 that are not present in any of the other arrays i.e. array2, array3.

Value Type: Array

Example:

<?php
function user_key_compare_func($x, $y)
{
if ($x === $y) 
{
return 0;
}
return ($x > $y)? 1:-1;
}
$array1 = array('Math'=>77, 20, 'Geography'=>89, 30, 'Language'=>67);
$array2 = array('Math'=>77, 'science'=>91 ,20, 'History'=>71);
$diff_result = array_diff_uassoc($array1, $array2, "user_key_compare_func");
print_r($diff_result);
?> 

Output:

Array([Geography]=>89[1]=>30[Language]=>67)

Pictorial Presentation:

php function reference: array_diff_uassoc() function

View the example in the browser

Practice here online:

See also

PHP Function Reference

Previous:array_diff_key
Next: array_diff_ukey



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);