PHP : array_diff_key() function
PHP: Computes the difference of arrays using keys for comparison
The array_diff_key() function is used to compare the keys from an array against the keys from another array 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(array1, array2, array3 ...)
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 second array. | Optional | Array |
Return value:
An array containing all the entries from array1 whose keys are not present in any of the other arrays i.e. array2, array3 etc..
Value Type: Array
Example:
<?php
$array1 = array('Math'=>77, 20, 'Geography'=>89, 'Language'=>67);
$array2 = array('Language'=>71, 'Science'=>91 ,20, 'Geography'=>89);
var_dump(array_diff_key($array1, $array2));
?>
Output:
array(1){["Math"]=> int(77) }
Pictorial Presentation:

View the example in the browser
Practice here online:
See also
Previous:array_diff_assoc
Next: array_diff_uassoc
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