Saturday, December 13, 2025
HomeLanguagesPHP array_​diff_​ukey() Function

PHP array_​diff_​ukey() Function

The array_​diff_​ukey() function is an inbuilt function in PHP that is used to execute the difference of arrays by using a callback function on the keys for comparison. This function compares the key from the array against the keys from arrays and returns the difference of that.

Syntax:

array array_diff_ukey(array $array, 
    array ...$arrays, callable $key_compare_func)

Parameters: This function accepts the following parameters that are described below:

  • $array: This parameter holds the array to compare from.
  • $arrays: This parameter holds the arrays to compare against.
  • $key_compare_func: It is a comparison callback function that returns an integer value based on the arguments.

Return Value: This function returns an array that contains all the entries from an array and is not present in any other arrays.

Example 1:

PHP




<?php
  
function key_compare_func($key1, $key2) {
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}
  
$arr1 = array(
    'A'  => 15, 
    'B'  => 12, 
    'C'  => 34, 
    'D' => 14
);
  
$arr2 = array(
    'B' => 50, 
    'D' => 60, 
    'E' => 70, 
    'F'   => 80
);
  
var_dump(array_diff_ukey($arr1, $arr2, 'key_compare_func'));
?>


Output:

array(2) {
      ["A"] => int(15)
      ["C"] => int(34)
}

Example 2:

PHP




<?php
  
function key_compare_func($key1, $key2) {
    if ($key1 == $key2)
        return 0;
    else if ($key1 < $key2)
        return 1;
    else
        return -1;
}
  
$arr1 = array(
    'Geeks'  => "HTML"
    'GFG'  => "CSS"
    'Geek'  => "JavaScript"
    'G4G' => "PHP"
);
  
$arr2 = array(
    'Geeks' => "CPP"
    'G4G' => "Java"
    'Geek' => "Python"
    'neveropen'   => "DSA"
);
  
var_dump(array_diff_ukey($arr1, $arr2, 'key_compare_func'));
?>


Output:

array(1) {
      ["GFG"] => string(3) "CSS"
}

Reference: https://www.php.net/manual/en/function.array-diff-ukey.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS