PHP array_diff_key() Function

Usage — The PHP array_diff_key() function is used to compute the difference of two or more arrays using their keys for comparison.

It has the following syntax:

PHP

array array_diff_key ( array $array1 , array $array2 [, array $... ] )

Here is an example of using array_diff_key():

PHP

$a1=array("a"=>"Apple","b"=>"Banana","c"=>"Orange","d"=>"Mango");
$a2=array("a"=>"Apple","b"=>"Banana","d"=>"Mango");
$result=array_diff_key($a1,$a2);

print_r($result);
/* Output —
Array
(
    [c] => Orange
) */

Return Value — This function returns an array containing all the entries from array1 whose keys are not present in any of the other arrays.

Additional Information — This function is like array_diff() except the comparison is done on the keys instead of the values. Just like array_diff_assoc(), this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]);.

PHP Version and Changelog — The array_diff_key() function is available in PHP 5 >= 5.1.0, PHP 7.

Relevant Functions — Other related PHP functions that you should know about are: array_diff() which computes the difference of arrays, array_udiff() which computes the difference of arrays by using a callback function for data comparison, array_diff_assoc() which computes the difference of arrays with additional index check, array_diff_uassoc() which computes the difference of arrays with additional index check which is performed by a user supplied callback function, array_udiff_assoc() which computes the difference of arrays with additional index check, compares data by a callback function, array_udiff_uassoc() which computes the difference of arrays with additional index check, compares data and indexes by a callback function, array_diff_ukey() which computes the difference of arrays using a callback function on the keys for comparison, array_intersect() which computes the intersection of arrays, array_intersect_assoc() which computes the intersection of arrays with additional index check, array_intersect_uassoc() which computes the intersection of arrays with additional index check, compares indexes by a callback function, array_intersect_key() which computes the intersection of arrays using keys for comparison and array_intersect_ukey() which computes the intersection of arrays using a callback function on the keys for comparison.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

array1

The array1 parameter is used to specify the array to compare from. This is a required parameter.

array2

The array2 parameter is used to specify an array to compare against. This is also a required parameter.

The ... parameter is used to specify more arrays to compare against. This is an optional parameter.

Working Examples

Here are some examples of using the array_diff_key() function:

PHP

$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);

print_r(array_diff_key($array1, $array2));
/* Output —
Array
(
    [red] => 2
    [purple] => 4
) */

Additional Tips

Here are some of the most upvoted tips taken from the comment section of the PHP manual:

  1. To return the unique elements (those with a key that exists only once in either array but not in both) try:

    PHP

    function array_unique_diff ($array1, $array2) {
        return array_merge(array_diff_key($array1, $array2), array_diff_key($array2, $array1));
    }
    $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
    
    print_r(array_diff_key($array1, $array2));
    /* Output —
    Array
    (
        [red] => 2
        [purple] => 4
    ) */
    
    print_r(array_diff_key($array2, $array1));
    /* Output —
    Array
    (
        [yellow] => 7
        [cyan] => 8
    ) */
    

    Suggested by - Ashton

Further Reading

  1. You can read more about the PHP array_diff_key() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top