array_diff_ukey in PHP

Syntax of array_diff_ukey

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

Returns an array containing all the values in array1 whose keys are not present in any of the other provided arrays. The function function is used to compare the keys 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. The keys of the values are preserved

Example of array_diff_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_diff_ukey($array1, $array2, 'key_compare_func'));
?>

Output

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

Leave a Comment