PHP array diff ukey

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

PHP: array_diff_ukey() function

PHP: Compares the keys from an array against the keys from another

The array_diff_ukey() function is used to compare two or more arrays using an additional user supplied function on the keys for comparison.The function compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Version

(PHP 4 and above)

Syntax

array_diff_ukey(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 must return 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 abc($ukey1, $ukey2)  

  2. {  

  3. if ($ukey1 == $ukey2)  

  4. return 0;  

  5. else if ($ukey1 > $ukey2)  

  6. return 1;  

  7. else  

  8. return -1;  

  9. }  

  10. $array1 = array('Orange' => 1, 'Apple' => 2, 'Banana' => 3, 'Chery'=> 4);  

  11. $array2 = array('Banana' => 5, 'Apple' => 6, 'Mango' => 7, 'Guava'=> 8);  

  12. var_dump(array_diff_ukey($array1, $array2, 'abc'));  

  13. ?>  

Output :

array(2) { ["Orange"]=> int(1) ["Chery"]=> int(4) }