PHP array_diff_key() Function

What is PHP array_diff_key() Function?

If you want to know what keys exist in one array and that doesn’t exist in other arrays (one or more arrays), use array_diff_key() function. This difference is made in respect to keys only. So, one element is considered exist if that element with same key is found in another array and the value may be same or different.

Syntax:

array_diff_key(array1, array2, array3, …)

Parameters:

The Function has 2 required parameters and any number of optional parameters-

array1 (Required): This is the first array whose keys the function checks whether exist in other arrays or not.

array2 (Required): It is another array whose keys the function compares with the first array. Check example 1.

array3, … (Optional): It is one or more arrays whose keys the function compares with the first array. Check example 2.

Return Values:

The function returns an array which contains keys that exist in first array and don’t exist in other arrays.

  • If keys are same but values are not same in the comparing arrays, the function considers it non-exists. Check example 3.
  • If values and keys are same but key cases are different in the comparing arrays, the function considers it non-exists. Check example 4.

Examples:

Example 1:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP'];
$last_array = array_diff_key($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [g] => Go
    [n] => NoSQL
)

Example 2:

<pre>
<?php
$array1 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java', 'n' => 'NoSQL'];
$array2 = ['c' => 'CSS', 'h' => 'HTML',  'j' => 'JavaScript', 'p' => 'PHP'];
$array3 = ['c' => 'CSS', 'c' => 'C#', 'g' => 'Go', 'j' => 'Java'];
$last_array = array_diff_key($array1, $array2, $array3);
print_r($last_array);
?>
</pre>

Output:

Array
(   
[n] => NoSQL
)

Example 3:

<pre>
<?php
$array1 = ['c' => 'CSS', 'j' => 'JavaScript'];
$array2 = ['s' => 'CSS', 's' => 'JavaScript'];
$last_array = array_diff_key($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    => CSS
    [j] => JavaScript
)

Example 4:

<pre>
<?php
$array1 = ['c' => 'CSS', 'j' => 'JavaScript'];
$array2 = ['C' => 'CSS', 'J' => 'Javascript'];
$last_array = array_diff_key($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    => CSS
    [j] => JavaScript
)

Example 5:

<pre>
<?php
$array1 = ['j' => 'JavaScript', 'c' => ['C', 'C++', 'C#'], 'p' => 'PHP'];
$array2 = ['c' => 'CSS', 'c' => ['C', 'C++']];
$last_array = array_diff_key($array1['c'], $array2['c']);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [2] => C#
)

Example 6:

<pre>
<?php
$array1 = ['HTML', 'JavaScript', 'PHP', 'PHP', 'CSS', 'NoSQL'];
$array2 = ['HTML', 'JavaScript', 'jQuery'];
$last_array = array_diff($array1, $array2);
print_r($last_array);
?>
</pre>

Output:

Array
(
    [2] => PHP
    [3] => PHP
    [4] => CSS
    [5] => NoSQL
)

Notes on array_diff_key() Function:

  • The function checks elements in one-dimension. If you want to compare elements inside inner levels of multidimensional arrays, you can mention those as the parameters in the function. Check example 5.
  • Repeating elements also repeats in the resultant array. Check example 6.

PHP Version Support:

PHP 5 >= 5.1.0, PHP 7, PHP 8

Summary: PHP array_diff_key() Function

array_diff_key() is a built-in array function which is a very effective function to compare element keys between arrays.

Reference:

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