Friday, October 24, 2025
HomeLanguagesPHP | array_intersect_ukey() Function

PHP | array_intersect_ukey() Function

The array_intersect_ukey() function is an inbuilt function in PHP which is used to compute the intersection of two or more array against keys with the help of user-defined function and the function return an array. The returned array is the first array, which is matching keys and present in all parameters is the final output. 

Syntax: 

array array_intersect_ukey( $array1, $array2, ..., $ukey_intersectFunction )

Parameters: This function accepts two or more arrays and a $ukey_intersectFunction parameters as mentioned above and described below: 

  • $array1: It is the initial array used to compare with other array. It is mandatory parameter.
  • $array2: The array compared with the first array key. It is mandatory parameter.
  • $array. . . : The list of array arguments to compared with the first array key. It is optional parameters.
  • $ukey_intersectFunction: It is required parameter which holds the user-defined function. A string that defines a callable comparison function.

Return Value: It returns an array containing the value of the first array whose keys exist in all others arguments.

Note: This function uses a user-defined function to compare the keys. Not compare values of keys.

Below examples illustrate the array_intersect_ukey() function in PHP:

Program 1: This program uses two array (array1 and array2) and a user-defined key comparison function (ukey_intersectFunction) to compute the intersection of arrays.

PHP




<?php
  
// PHP program to illustrates the
// array_intersect_ukey() function
function ukey_intersectFunction($x, $y)
{       
   if ($x === $y)
      return 0;
   return ($x > $y)? 1: -1;
}
  
$a1 = array(
    "a" => "Java",
    "b" => "C++",
    "c" => "XML",
    "d" => "Python",
    "p" => "PHP"
);
$a2 = array(
    "a" => "Python",
    "b" => "CSS",
    "h" => "VB",
    "x" => "HTML",
    "p" => "SQL"
);
  
// Print results if the key is matching from both
// array and print first array elements.
$result = array_intersect_ukey($a1, $a2, "ukey_intersectFunction");
print_r($result);
?>


Output: 

Array
(
    [a] => Java
    [b] => C++
    [p] => PHP
)

 

Program 2: This program uses four array(array1, array2, array3 and array4) and a user-defined key comparison function (ukey_intersectFunction) to compute the intersection of arrays.

PHP




<?php
  
// PHP program to illustrates the
// array_intersect_ukey() function
function ukey_intersectFunction($x, $y)
{       
   if ($x === $y)
      return 0;
   return ($x > $y)? 1: -1;
}
  
// Arrays
$arr1 = array(
    "a" => "A",
    "b" => "B",
    "c" => "D",
    "d" => "E",
    "p" => "F"
);
$arr2 = array(
    "o" => "A",
    "b" => "B",
    "h" => "V",
    "x" => "T",
    "p" => "X"
);
$arr3 = array(
    "a" => "N",
    "b" => "B",
    "h" => "O",
    "x" => "T",
    "p" => "P"
);
$arr4 = array(
    "s" => "W",
    "b" => "B",
    "z" => "D",
    "x" => "T",
    "p" => "P"
);
  
// Print the result if key is matching from both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
   $arr2, $arr3, $arr4, "ukey_intersectFunction");
  
print_r($result);
  
?>


Output: 

Array
(
    [b] => B
    [p] => F
)

 

Program 3: This program uses three arrays (array1, array2, and array3) and a user-defined key comparison function to compute the intersection of arrays.

PHP




<?php
  
// PHP program to illustrates the
// array_intersect_ukey() function
function ukey_intersectFunction($x, $y)
{       
   if ($x === $y)
      return 0;
   return ($x > $y)? 1: -1;
}
  
// Arrays
$arr1 = array(
    "a" => "Bhopal",
    "b" => "New Delhi",
    "n" => "Madras",
    "o" => "Mumbai",
    "p" => "Finland"
);
$arr2 = array(
    "a" => "Bhubaneswar",
    "b" => "Ranchi",
    "n" => "Kolkata",
    "o" => "Lucknow",
    "p" => "Allahabad"
);
$arr3 = array(
    "a" => "Rourkela",
    "b" => "Jammu",
    "n" => "Kashmir",
    "o" => "Tripura",
    "p" => "Shillong"
);
  
// Print the result if key is matching from both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
      $arr2, $arr3, "ukey_intersectFunction");
print_r($result);
?>


Output: 

Array
(
    [a] => Bhopal
    [b] => New Delhi
    [n] => Madras
    [o] => Mumbai
    [p] => Finland
)

 

Program 4: This program uses two array (array1 and array2), and a user-defined key comparison function (ukey_intersectFunction) to compute the intersection of arrays. There are both array have different key then array return NULL/Empty Array.

PHP




<?php
  
// PHP program to illustrates the
// array_intersect_ukey() function
function ukey_intersectFunction($x, $y)
{       
   if ($x === $y)
      return 0;
   return ($x > $y)? 1: -1;
}
  
// Arrays
$arr1 = array(
    "a" => "IIT Shillong",
    "b" => "IIT Bhopal",
    "n" => "NIT Bhopal"
);
$arr2 = array(
    "x" => "IIT Bhubaneswar",
    "y" => "IIT Ranchi",
    "z" => "NIT Durgapur"
);
  
  
// Print the result if key is matching from both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
print_r($result);
?>


Output: 

Array
(
)

 

Program 5: This program uses two array (array1 and array2) and both array have different key but same value in the same index. Compare the array and return the NULL/Empty Array.

PHP




<?php
  
// PHP program to illustrates the
// array_intersect_ukey() function
function ukey_intersectFunction($x, $y)
{       
   if ($x === $y)
      return 0;
   return ($x > $y)? 1: -1;
}
  
// Arrays
$arr1 = array(
    "a" => "IIT",
    "b" => "CIT",
    "n" => "NIT"
);
$arr2 = array(
    "x" => "IIT",
    "y" => "CIT",
    "z" => "NIT"
);
  
  
// Print the result if key is matching from both
// array and print first array elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
  
print_r($result);
?>


Output: 

Array
(
)

 

Related Articles: 

Reference: http://php.net/manual/en/function.array-intersect-ukey.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS