PHP array_intersect_ukey Function: How it Works with Examples

PHP array_intersect_ukey

The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys only.

Understand the array_intersect_ukey in PHP

The array_intersect_ukey function compares keys of two or more arrays. It checks matches with a user-defined compare function.

The syntax looks like this:

array_intersect_ukey(array $array1, array $array2, callable $key_compare_func)

Here is how it works:

  • array1: The first array.
  • array2: The second array.
  • key_compare_func: A function that decides how to compare keys.

The function runs the compare function for each key pair. It keeps only keys that match.

For a quick example:

function compareKeys($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}

$arr1 = ["a" => "red", "b" => "blue", "c" => "green"];
$arr2 = ["a" => "yellow", "b" => "pink", "d" => "black"];

$result = array_intersect_ukey($arr1, $arr2, "compareKeys");
print_r($result);

This code compares keys with compareKeys. It finds only keys that match both arrays. The output will be:

Array
(
[a] => red
[b] => blue
)

So, how does the user-defined compare function work?

The compare function must return:

  • 0 if keys are equal.
  • 1 if the first key is greater.
  • -1 if the first key is smaller.

PHP uses this return value to decide if keys match.

The Difference Between array_intersect and array_intersect_ukey

The array_intersect function:

This function compares array values. It keeps only values that appear in all arrays.

The array_intersect_ukey function:

This function compares array keys. It needs a user-defined compare function to match keys.

Here is a table that shows you the key differences:

FunctionComparison BaseNeeds Compare FunctionKeeps Keys or Values
array_intersectValuesNoValues
array_intersect_ukeyKeysYesKeys

Use array_intersect to find common values. Use array_intersect_ukey to find common keys.

Examples of array_intersect_ukey in PHP

Match Keys in Two Arrays

function compareKeys($a, $b) {
    return $a <=> $b;
}

$arr1 = ["x" => 10, "y" => 20, "z" => 30];
$arr2 = ["x" => 15, "y" => 25, "w" => 40];

$result = array_intersect_ukey($arr1, $arr2, "compareKeys");
print_r($result);

This example matches keys x and y only. It ignores values. The output is ["x" => 10, "y" => 20]. This shows that the function filters by keys alone.

Compare Three Arrays by Keys:

function keyCompare($a, $b) {
    return $a <=> $b;
}

$arr1 = ["a" => 1, "b" => 2, "c" => 3];
$arr2 = ["a" => 10, "b" => 20, "d" => 40];
$arr3 = ["a" => 100, "b" => 200, "e" => 500];

$result = array_intersect_ukey($arr1, $arr2, $arr3, "keyCompare");
print_r($result);

This example uses three arrays. It keeps only keys a and b because they appear in all arrays.

The output:

Array
(
[a] => 1
[b] => 2
)

Custom Sort Logic in Compare Function:

function reverseCompare($a, $b) {
    return $b <=> $a;
}

$arr1 = ["c" => 3, "b" => 2, "a" => 1];
$arr2 = ["a" => 5, "b" => 6, "c" => 7];

$result = array_intersect_ukey($arr1, $arr2, "reverseCompare");
print_r($result);

This example compares keys with reverse logic. All three keys match, so the result includes all pairs, and the compare function changes order logic but the match still happens.

Here is the output:

Array
(
[c] => 3
[b] => 2
[a] => 1
)

No Matches Found:

function compareKeysStrict($a, $b) {
    return $a <=> $b;
}

$arr1 = ["m" => 10, "n" => 20];
$arr2 = ["x" => 30, "y" => 40];

$result = array_intersect_ukey($arr1, $arr2, "compareKeysStrict");
print_r($result);

This example shows a case where no keys match. The result is an empty array. It proves the function only keeps keys that exist in all arrays.

Wrapping Up

You learned what array_intersect_ukey does and how to use it.

Here is a quick recap:

  • The function compares arrays by keys with a user-defined compare function.
  • It returns only keys that exist in all arrays.
  • The compare function must return 0, 1, or -1.
  • array_intersect works with values. array_intersect_ukey works with keys.
  • Use cases differ based on whether you need a value match or a key match.

FAQs

What does PHP array_intersect_ukey do?

PHP array_intersect_ukey compares array keys using a custom callback function. It returns keys present in all arrays.
function compareKeys($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "black", "c" => "yellow", "d" => "brown");
$result = array_intersect_ukey($array1, $array2, "compareKeys");
print_r($result);

How does array_intersect_ukey differ from array_intersect?

  • array_intersect compares array values.
  • array_intersect_ukey compares array keys with a callback.

What is the syntax of PHP array_intersect_ukey?

array array_intersect_ukey ( array $array1 , array $array2 [, array $... ], callable $key_compare_func )
  1. $array1 is the main array.
  2. $array2 and more arrays are compared.
  3. $key_compare_func is user-defined for key comparison.

Can you give a real use case of array_intersect_ukey?

Example: When you need to filter arrays by common keys like user IDs.
function keyCompare($a, $b) {
    return strcmp($a, $b);
}
$users1 = array("101" => "Alice", "102" => "Bob");
$users2 = array("101" => "John", "103" => "Sara");
$result = array_intersect_ukey($users1, $users2, "keyCompare");
print_r($result);

Similar Reads

PHP For Loop: Run Code Using a Counter with Examples

You may need to repeat tasks in PHP. The PHP for loop solves this by letting you run code many…

PHP htmlspecialchars Function: Prevent XSS in HTML Output

A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

PHP Multidimensional Arrays: Data Manipulation

In web development, managing and manipulating data is one of the most common tasks. When it comes to storing complex…

PHP abs Function: How to Get Absolute Values

The abs() function in PHP gives you a number without its sign — it always returns a positive value. That…

Understanding the PHP Enumerable

Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

PHP Functions: The Complete Guide for Beginners

So, what is a function? Quite simply, a function in PHP is a set of instructions you write once, and…

PHP array_intersect Function: How it Works with Examples

The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…

PHP Anonymous Function: How It Works with Examples

The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

Class and Object in PHP: How They Work with a Real Example

Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

PHP Elvis Operator: How (?:) Works with Examples

The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…

Previous Article

JavaScript Popup Boxes: How they Works with Examples

Next Article

HTML5 New Elements with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.