English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_intersect_ukey() function usage and example

PHP Array Function Manual

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

Syntax

array_intersect_ukey ( $array1, $array2 [, $array3..., callback $key_compare_func] );

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 keys present in the compared arrays (array1) in, as well as in any other parameter array (array2 Or array3 etc.) key names.

Parameter

Serial numberParameter and Description
1

array1(required)

The first array is the array that other arrays will be compared with.

2

array2(required)

This is the array to be compared with the first array

3

array3(optional)

This is the array to be compared with the first array

4

key_compare_func (required)

User-defined callback function.

Return value

It returns an array containing array1An array of all values that exist in the match keys of all parameters. If there are any errors, it will return FALSE.

Online example

<?php
   function key_compare_func($k1, $k2) {
      if ($k1 == $k2)
         return 0;
      
      else if ($k1 > $k2)
         return 1;
      
      else
         return -1;
   }
   $input1 = array('blue'=>1, 'red'=>2, 'green'=>3, 'purple'=>4);
   $input2 = array('green'=>5, 'blue'=>6, 'pink'=>7, 'black'=>8);
   
   $result = array_intersect_ukey($input1, $input2, "key_compare_func");
   var_dump($result);
?>
Test and see‹/›

Output result:

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}

PHP Array Function Manual