Tutorialspoint.dev

PHP | array_intersect_ ukey() Function

The array_intersect_ ukey() function is used to compare two or more array against keys, by 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_intersect_ ukey($array1, $array2, $array3……., ukey_intersectFunction)

Parameters Used:
This function accepts two parameters First parameter is an array list ($array1, $array2, $array3, ………. $arraynth) and minimum two arrays are required. Second parameter is a user-defined function(ukey_intersectFunction) The parameters are described below:

  1. array1 : It is the initial array and compares to another array. It is Mandatory .
  2. array2 : The array Compared with the first array key. It is Mandatory .
  3. array3 : The second array Compared with the first array key. It is Optional.
  4. ukey_intersectFunction : It is Required user-defined function. A string that defines a callable comparison function.

Return Value:
Returns an array types values which contain the first array whose keys is exits in all others arguments.

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



Let’s take some examples to understand the array_intersect_ ukey() Function.

Program 1:
Take two array (array1 and array2) and using user-defined key comparison function (ukey_intersectFunction).

<?php
// PHP array_intersect_ ukey() function
// program
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 result wich key is matching form both
// array and print first arary elements.
$result = array_intersect_ukey($a1, $a2, "ukey_intersectFunction");
print_r($result);
?>

Output:

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

Program 2:
Take four array(array1, array2, array3 and array4) and using user-defined key comparison function (ukey_intersectFunction).

<?php
// PHP array_intersect_ ukey() function
// program
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 result wich key is matching form both
// array and print first arary elements.
$result = array_intersect_ukey($arr1,
   $arr2, $arr3, $arr4, "ukey_intersectFunction");
  
print_r($result);
  
?>

Output:

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

Program 3:
Take three arrays (array1, array2, and array3) and using user-defined key comparison function, all have the same key and different values. Return and display the result of array1.

<?php
// PHP array_intersect_ ukey() function
// program
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 result wich key is matching form both
// array and print first arary 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:
Take two array (array1 and array2), using user-defined key comparison function(ukey_intersectFunction). There are both array have different key then array return NULL/Empty Array .

<?php
// PHP array_intersect_ ukey() function
// program
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 result wich key is matching form both
// array and print first arary elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
print_r($result);
?>

Output:

Array
(
)

Program 5:
Take two array (array1 and array2), There both array have different key But same value in same index, compare array return NULL/Empty Array.

<?php
// PHP array_intersect_ ukey() function
// program
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 result wich key is matching form both
// array and print first arary elements.
$result = array_intersect_ukey($arr1,
         $arr2, "ukey_intersectFunction");
  
print_r($result);
?>

Output:

Array
(
)

Related Articles:

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



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter