PHP array diff uassoc

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

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

  1. function user_key_compare_func($x, $y)  

  2. {  

  3. if ($x === $y)   

  4. {  

  5. return 0;  

  6. }  

  7. return ($x > $y)? 1:-1;  

  8. }  

  9. $array1 = array('Math'=>77, 20, 'Geography'=>89, 30, 'Language'=>67);  

  10. $array2 = array('Math'=>77, 'science'=>91 ,20, 'History'=>71);  

  11. $diff_result = array_diff_uassoc($array1, $array2, "user_key_compare_func");  

  12. print_r($diff_result);  

  13. ?>   

Output :

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