PHP array_intersect_ukey() function

Example

Compare the key names of two arrays (using a user-defined function to compare key names) and return the intersection:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>

Run Instances

Definition and Usage

The array_intersect_ukey() function is used to compare the key names of two (or more) arrays and return the intersection.

Note:This function uses a user-defined function to compare key names!

This function compares the key names of two (or more) arrays and returns an intersection array that includes all the elements in the compared arrays (array1)are also present in any other parameter arrays (array2 or array3 etc.) key names.

Description

The array_intersect_ukey() function calculates the intersection of arrays using a callback function to compare key names.

The array_intersect_ukey() function returns an array that contains all the keys that appear array1 the value of the key name that is present in both the array and simultaneously appears in all other parameter arrays.

This comparison is performed through a callback function provided by the user. The function takes two parameters, which are the key names to be compared. If the first parameter is less than the second parameter, the function should return a negative number, if the two parameters are equal, it should return 0, and if the first parameter is greater than the second parameter, it should return a positive number.

syntax

array_intersect_ukey(array1,array2,array3...myfunction)
Parameters Description
array1 Required. The first array to compare with the other arrays.
array2 Required. The array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.
myfunction Required. A string that defines the callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0.

Technical Details

Return Value: Returns an intersection array that includes all the key names in the arrays being compared (array1)are also present in any other parameter arrays (array2 or array3 etc.) key names.
PHP Version: 5.1.0+

More Examples

Example 1

Compare the key names of three arrays (using a user-defined function to compare key names) and return the intersection:

<?php
function myfunction($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");
$result=array_intersect_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
?>

Run Instances