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

Tutorial Básico do PHP

Tutorial Avançado do PHP

PHP & MySQL

Manual de Referência do PHP

PHP array_intersect_ukey() function usage and example

PHP Array Function Manual

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

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

Parameter

Serial NumberParameters and Description
1

array1(required)

The first array is the array to be compared with the other arrays.

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 the array1An array of all values that have matching keys in 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