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 array_merge: Combine Multiple Arrays into One

You have two or more arrays. You want one. That is the problem array_merge solves in PHP. This function lets…

PHP require_once and require

Essentially, “require” and “require_once” are directives in PHP to include and evaluate a specific file during the execution of a…

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 compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

Check If a Row Exists in PDO

At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…

MongoDB PHP Driver: Install and Get Started

Traditional relational databases like MySQL often take center stage. However, with the rise of NoSQL databases, MongoDB has emerged as…

Understanding the PHP Exclusive OR (XOR) Operator

In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

PHP Syntax: Standard Tags, Echo, and Short Tags

Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

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.