PHP array_diff_ukey() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_diff_ukey() compares the keys of the first array with the keys of other arrays by using a callback function that you provide. It returns the entries whose keys have no match in the other arrays.

You use array_diff_ukey() when keys should be matched by a custom rule, such as ignoring case or looking only at the first letter. Unlike array_diff_uassoc(), the values are not compared at all.

Syntax

Call array_diff_ukey() with the values you need. Start with this line.

PHP
$result = array_diff_ukey($a, $b, 'strcasecmp');
CallWhat it does
array_diff_ukey($a, $b, 'strcmp')Compares keys exactly, like array_diff_key() does.
array_diff_ukey($a, $b, 'strcasecmp')Compares keys without caring about upper and lower case.
array_diff_ukey($a, $b, fn($x, $y) => $x <=> $y)Compares keys with your own function.

The callback is the last argument. It receives two keys and must return 0 for equal keys, a negative number when the first key is smaller and a positive number when it is larger. The values of all arrays are ignored.

Library declarations
PHP
array_diff_ukey(array $array, array ...$arrays, callable $key_compare_func): array

Parameters

ParameterDescription
arrayRequired. The array to compare from.
arraysOptional. One or more arrays whose keys are compared with the first array.
key_compare_funcRequired. The callback that compares two keys. It must return an integer: 0 for equal, negative or positive for smaller or larger.

Return Value

Return ValueDescription
arrayThe entries of the first array whose keys do not match any key in the other arrays.

PHP Examples

Example 1 — Comparing Keys With strcmp

Use strcmp as the callback to compare the keys exactly.

PHP
<?php
$a = ['blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4];
$b = ['green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8];

print_r(array_diff_ukey($a, $b, 'strcmp'));

How It Works

  1. strcmp returns 0 only when two keys are exactly the same.
  2. The keys 'blue' and 'green' exist in both arrays, so they are removed.
  3. The keys 'red' and 'purple' do not exist in $b, so they are kept.
  4. The values of $b, such as 5 and 6, play no role in the result.

Example 2 — Ignoring Case

With strcasecmp, keys that differ only in letter case are treated as the same key.

PHP
<?php
$a = ['Red' => 1, 'blue' => 2];
$b = ['RED' => 9];

print_r(array_diff_ukey($a, $b, 'strcasecmp'));

How It Works

  1. 'Red' and 'RED' are equal when the case is ignored.
  2. The entry 'Red' => 1 is therefore removed, and the value 9 does not matter.
  3. 'blue' has no match in $b, so it stays.
  4. The result is ['blue' => 2].

Example 3 — A Custom Rule: First Letter

Write a callback that treats two keys as equal when they start with the same letter.

PHP
<?php
$a = ['apple' => 1, 'banana' => 2, 'cherry' => 3];
$b = ['avocado' => 0, 'cranberry' => 0];

$sameFirstLetter = fn($k1, $k2) => strcmp($k1[0], $k2[0]);

print_r(array_diff_ukey($a, $b, $sameFirstLetter));

How It Works

  1. The callback compares only the first character of each key.
  2. 'apple' matches 'avocado' because both start with 'a'.
  3. 'cherry' matches 'cranberry' because both start with 'c'.
  4. Only 'banana' has no match, so it is the only entry left.

Frequently Asked Questions

It returns the entries of the first array whose keys are not found in the other arrays, using your own callback to decide whether two keys are equal.
No. Only keys are compared. Use array_diff_uassoc() when the values must match as well.
An integer. 0 means the keys are equal, a negative number means the first key is smaller and a positive number means it is larger.
array_diff_key() uses the built in key comparison. array_diff_ukey() lets you decide how keys are compared with a callback, for example to ignore case.
The callback is the last argument, after all the arrays you want to compare.

Next: array_fill()

array_fill() creates an array that contains the same value repeated a number of times. You.

array_fill() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful