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", 'zambiatek' => "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, zambiatek 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!



