PHP array intersect ukey

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

PHP: array_intersect_ ukey() function

PHP: Computes the intersection of arrays using a callback function

The array_intersect_ukey() function is used to compare two or more arrays, against keys, using a user-supplied function.

The function returns an array containing all the values of the first array which have matching keys that are present in all the arguments.

Version

(PHP 4 and above)

Syntax

array_intersect_ukey(array_name1, array_name2, array_name3.......user-defined-function)

Parameters

Name

Description

Required /
Optional

Type

array_name1

Initial array for comparison with other arrays.

Required

Array

array_name2

The first array to compare with keys of array_name1.

Required

Array

array_name3

The second array to compare with keys of array_name1.

Optional

Array

user-defined-function

User supplied function to do the comparison.

Required

Function

Return value

The values of array_name1 whose keys exist in all the arguments.

Value Type : Array

Pictorial Presentation :

php array array_intersect_ukey() function

Example :

  1. function user_function($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("Orange" => 5, "Banana" => 6, "Mango" => 7, 'Guava'=> 8);  

  12. var_dump(array_intersect_ukey($array1, $array2,"user_function"));  

  13. ?>  

Output :

array(2) { ["Orange"]=> int(1) ["Banana"]=> int(3) }