PHP Function Reference

PHP array_diff_key() Function



The PHP array_diff_key() function compares keys of an array against one or more other arrays and returns all elements of the first array that are not present in any of the other arrays.

The array_diff() function is like this function except the comparison is done on the values instead of the keys.

Syntax

array_diff_key(array, arrays)

Parameters

array Required. Specify an array to compare from.
arrays Required. Specify one or more arrays to compare against.

Return Value

Returns an array containing all the entries from array whose keys are absent from all of the other arrays.

Exceptions

NA.

Example:

The example below shows the usage of array_diff_key() function.

<?php
$Arr1 = array("a"=>"Red",
              "b"=>"Red",
              "c"=>"Blue",
              "d"=>"Blue",
              "e"=>"Green");
$Arr2 = array("a"=>"Red", 
              "b"=>"Blue", 
              "c"=>"Green");

//comparing Arr1 against Arr2
$Arr1_diff_Arr2 = array_diff_key($Arr1, $Arr2);
print_r($Arr1_diff_Arr2);
?>

The output of the above code will be:

Array
(
    [d] => Blue
    [e] => Green
)

❮ PHP Array Reference