Skip to content
Related Articles

Related Articles

PHP | array_intersect_ukey() Function
  • Last Updated : 19 Nov, 2019

The array_intersect_ukey() function is in 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 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 form 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 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 form 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 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" => "Kolkatta",
    "o" => "Luknow",
    "p" => "Allahabad"
);
$arr3 = array(
    "a" => "Rourkela",
    "b" => "Jammu",
    "n" => "Kasmir",
    "o" => "Tripura",
    "p" => "Sillong"
);
  
// Print the result if key is matching form 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 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 Sillong",
    "b" => "IIT Bhopal",
    "n" => "NIT Bhopal"
);
$arr2 = array(
    "x" => "IIT Bhubaneswar",
    "y" => "IIT Ranchi",
    "z" => "NIT Durgapur"
);
  
  
// Print the result if key is matching form 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 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 form 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :