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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_diff_ukey() function usage and examples

PHP Array Function Manual

The PHP array_diff_ukey() function compares arrays and returns the difference set of two arrays (only compares the keys, using a user-defined key comparison function).

Definition and Usage

array_diff_ukey()uses a user-defined function to compare the keys of two (or more) arrays and returns an array that contains the keys of array1but not existing in array2or array3items in

This function is different fromarray_diff()the function because array_diff() compares values, while this function compares keys.

This function is different fromarray_diff_assoc()Function, because array_diff_assoc() uses an internal algorithm to compare indices, while this function uses a user-defined function.

Syntax

array_diff_ukey($array1, $array2 , $array3...,callback $key_compare_func];

Parameter

NumberParameters and Description
1

array1(Required)

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

2

array2(Required)

It is an array to be compared with the first array

3

array3(Optional)

It is an array to be compared with the first array

4

key_compare_func(Required)

The comparison function must return an integer less than, equal to, or greater than 0 when the first parameter is less than, equal to, or greater than the second parameter.

Return Value

This function returns an array containing all entries from the array1contains all entries that are not present in all other arrays.

PHP version

This function was first introduced in PHP version5.1introduced in version .0.

Online Example

array_diff_ukey() and key_compare_func() Example-

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }
   $input1 = array(0=>"banana", 1=>"orange", 2=>"grapes");
   $input2 = array(3=>"apple",1=>"apricot", 5=>"mango");
   print_r(array_diff_ukey($input1,$input2,"key_compare_func");
?>
Test and See‹/›

Output Result:

Array
(
    [0] => banana
    [2] => grapes
)

Online Example

array_intersect_ukey() Example-

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }
   $input1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
   $input2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
   print_r(array_diff_ukey($input1,$input2,"key_compare_func");
?>
Test and See‹/›

Output Result:

Array
(
    [red] => 2
    [purple] => 4
)

PHP Array Function Manual