PHP array_intersect_ukey() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_intersect_ukey() returns the items of the first array whose keys exist in all other arrays, using your own function to compare keys.

Only keys are compared, and the callback you pass decides when two keys count as equal. Values are ignored.

Syntax

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

PHP
$common = array_intersect_ukey($a, $b, "strcasecmp");
CallWhat it does
array_intersect_ukey($a, $b, "strcasecmp")Matches keys without caring about letter case.
array_intersect_ukey($a, $b, "strcmp")Matches keys exactly, like array_intersect_key().
array_intersect_ukey($a, $b, $myCompare)Uses your own function to compare keys.

This is the custom-callback version of array_intersect_key(). Use it only when plain key comparison is not enough.

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

Parameters

ParameterDescription
arrayRequired. The array to check. Its items are returned.
arraysOptional. One or more arrays whose keys are compared with the first array.
key_compare_funcRequired. A callable that compares two keys and returns an int. It must be the last argument.

Return Value

Return ValueDescription
arrayA new array with the items of the first array whose keys match in all other arrays.

PHP Examples

Example 1 — Match Keys Ignoring Case

Use strcasecmp so that Apple and apple are treated as the same key.

PHP
<?php
$a = ["Apple" => 1, "Banana" => 2, "Cherry" => 3];
$b = ["apple" => 9, "cherry" => 8];

$result = array_intersect_ukey($a, $b, "strcasecmp");

echo json_encode($result), PHP_EOL;

How It Works

  1. Build two arrays. The second one uses lowercase keys.
  2. Pass strcasecmp. It makes Apple equal to apple.
  3. Values are ignored. It does not matter that 1 and 9 differ.
  4. Read the result. The original keys and values of $a are returned.

Example 2 — Exact Compare Gives No Match

When the callback is strict, differently written keys do not match.

PHP
<?php
$a = ["Apple" => 1, "Banana" => 2];
$b = ["apple" => 1, "banana" => 2];

echo json_encode(array_intersect_ukey($a, $b, "strcmp")), PHP_EOL;
echo count(array_intersect_ukey($a, $b, "strcasecmp")), PHP_EOL;

How It Works

  1. Use keys that differ in case. Apple and apple are not the same string.
  2. Try strcmp. It is case-sensitive, so nothing matches and the result is [].
  3. Try strcasecmp. Now both keys match.
  4. Count the result. Two items are returned.

Example 3 — Filter Settings by Allowed Keys

Keep only the settings whose names appear in an allowed list, ignoring case.

PHP
<?php
$defaults = ["Theme" => "light", "Lang" => "en", "Debug" => false];
$allowed  = ["theme" => 1, "lang" => 1];

$settings = array_intersect_ukey($defaults, $allowed, "strcasecmp");

foreach ($settings as $name => $value) {
    echo $name . " = " . $value . PHP_EOL;
}

How It Works

  1. Define all settings. Some have capital letters.
  2. Define allowed names. They are written in lowercase.
  3. Intersect with strcasecmp. Debug is dropped because it is not allowed.
  4. Loop through the result. Each remaining key keeps its original spelling.

Frequently Asked Questions

It returns the items of the first array whose keys exist in all other arrays, using a callback you provide to compare the keys.
array_intersect_ukey(array $array, array ...$arrays, callable $key_compare_func): array. The callback comes after all the arrays.
No. Only the keys are compared. Use array_intersect_uassoc() when you also want the values to match.
Use it when keys need special matching, such as ignoring case. For normal exact keys, array_intersect_key() is simpler.
Any callable that takes two keys and returns an int, like "strcmp", "strcasecmp" or your own function.

Next: array_is_list()

array_is_list() checks whether an array is a list, meaning its keys are 0, 1, 2 and so on .

array_is_list() 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