The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys only.
Table of Content
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:
0if keys are equal.1if the first key is greater.-1if 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:
| Function | Comparison Base | Needs Compare Function | Keeps Keys or Values |
|---|---|---|---|
| array_intersect | Values | No | Values |
| array_intersect_ukey | Keys | Yes | Keys |
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?
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_intersectcompares array values.array_intersect_ukeycompares 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 )
- $array1 is the main array.
- $array2 and more arrays are compared.
- $key_compare_func is user-defined for key comparison.
Can you give a real use case of array_intersect_ukey?
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
Bitwise operators use symbols like &, |, and ^, and they work at the binary level. But once you understand…
JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…
PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…
If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…
PHP array_unshift helps you add new elements to appear before the existing ones. Understand the array_unshift function in PHP The…
actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…
The function_exists() function in PHP checks whether a given function is defined or not. The function_exists() function works for both…
The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…
Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…
PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…