(PHP 5 >= 5.1.0, PHP 7, PHP 8)
array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison
Parametersarray
Initial array for comparison of the arrays.
arrays
Arrays to compare keys against.
key_compare_func
The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
CautionReturning non-integer values from the comparison function, such as float, will result in an internal cast to int of the callback's return value. So values such as 0.99
and 0.1
will both be cast to an integer value of 0
, which will compare such values as equal.
Returns the values of array
whose keys exist in all the arguments.
Example #1 array_intersect_ukey() example
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>
The above example will output:
array(2) { ["blue"]=> int(1) ["green"]=> int(3) }
In our example you see that only the keys 'blue'
and 'green'
are present in both arrays and thus returned. Also notice that the values for the keys 'blue'
and 'green'
differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array
.
1 month ago
This is a cleaner and more modern approach (PHP 7.4+ syntax).
<?php
$array1
= ['blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4];
$array2 = ['green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8];$result = array_intersect_ukey($array1, $array2, fn($a, $b) => $a <=> $b);var_dump($result);?>
Explanation:
The comparison function fn($a, $b) => $a <=> $b uses the spaceship operator directly inside the array_intersect_ukey call.
The result will contain entries from $array1 whose keys also exist in $array2.
Output:
php
Copy
Edit
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}
gk at anuary dot com ¶
11 years ago
"array_intersect_ukey" will not work if $key_compare_func is using regular expression to perform comparison. "_array_intersect_ukey" fully implements the "array_intersect_ukey" interface and handles properly boolean comparison. However, the native implementation should be preferred for efficiency reasons.
<?php
$key_compare_func = function ($a, $b) {
var_dump('a=' . $a. '; b=' . $b);
if (
strpos($b, '/') === 0) {
return preg_match($b, $a) > 0 ? 0 : -1;
}
if (
$a == $b) {
$weight = 0;
} else if ($a > $b) {
$weight = 1;
} else {
$weight = -1;
}var_dump('literal comparison: ' . $weight);
return
$weight;
};$foo = ['aab' => '', 'bbb' => '', 'ccb' => '', 'abb' => ''];
$bar = ['/[a-z]b[a-z]/' => ''];
$buz = ['/c[a-z][a-z]/' => ''];
echo
PHP_EOL . 'array_intersect_ukey' . PHP_EOL . PHP_EOL;$subject = array_intersect_ukey ($foo, $bar, $buz, $key_compare_func);
echo
PHP_EOL;var_dump($subject);
echo
PHP_EOL . '_array_intersect_ukey' . PHP_EOL . PHP_EOL;$subject = _array_intersect_ukey($foo, $bar, $buz, $key_compare_func);
echo
PHP_EOL;var_dump($subject);function _array_intersect_ukey (array $arr1, array $arr2, $key_compare_func) {
$arr_matched = [];
$arr_unmatched = [];$args = func_get_args();$key_compare_func = end($args);
foreach (
$arr1 as $k1 => $v1) {
foreach ($arr2 as $k2 => $v2) {
$diff = $key_compare_func($k1, $k2);if ($diff === 0) {
$arr_matched[$k1] = $v1;
} else {
$arr_unmatched[$k1] = $v1;
}
}
}
if (
count($args) <= 3) {
return $arr_matched;
}array_splice($args, 0, 2, [$arr_unmatched]);
return
array_merge($arr_matched, call_user_func_array('_array_intersect_ukey', $args));
}?>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4