array_intersect_ukey in PHP

Syntax of array_intersect_ukey in PHP

array array_intersect_ukey(array array1, array array2
[, … array arrayN], callable function)

Returns an array consisting of every element in array1 whose keys also exist in every other array

The function function is used to compare the values of the elements for equality. The function is called with two parameters—the keys to compare. It should return an integer less than 0 if the first argument is less than the second, 0 if the first and second arguments are equal, and an integer greater than 0 if the first argument is greater than the second

Example of array_intersect_ukey

<?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'));
?>

Output

array(2) {
  ["blue"]=>
  int(1)
  ["green"]=>
  int(3)
}

Leave a Comment