PHP: array_diff_ukey() function
PHP: Compares the keys from an array against the keys from another
The array_diff_ukey() function is used to compare two or more arrays using an additional user supplied function on the keys for comparison.The function compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Version:
(PHP 4 and above)
Syntax:
array_diff_ukey(array1, array2, array3,.....user-defined-function)
Parameter:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array1 | The specified array which will be compared with other arrays. | Required | Array |
array2 | Compared with the first array. | Required | Array |
array3 | Compared with the first array. | Optional | Array |
user-defined-function | The specified callback function. The 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. | Required | - |
Return value:
An array containing all the entries from array1 that are not present in any of the other arrays i.e. array2, array3.
Value Type: Array
Example:
<?php
function abc($ukey1, $ukey2)
{
if ($ukey1 == $ukey2)
return 0;
else if ($ukey1 > $ukey2)
return 1;
else
return -1;
}
$array1 = array('Orange' => 1, 'Apple' => 2, 'Banana' => 3, 'Chery'=> 4);
$array2 = array('Banana' => 5, 'Apple' => 6, 'Mango' => 7, 'Guava'=> 8);
var_dump(array_diff_ukey($array1, $array2, 'abc'));
?>
Output:
array(2) {["Orange"]=> int(1)["Chery"]=> int(4)}
Pictorial Presentation:

View the example in the browser
Practice here online:
See also
Previous:array_diff_uassoc
Next: array_diff
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises