PHP array_diff_key() Function

The array_diff_key() function is an inbuilt PHP function that compares the keys of two or more arrays and returns the differences. This function was introduced in PHP 5.0.

Syntax


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

                    

Parameters

Parameter Description Is Compulsory
array1 The first array to compare. Compulsory
array2 The array to compare against. Compulsory
... Additional arrays to compare against. Optional

Return Values

The array_diff_key() function returns an array containing all the elements from array1 whose keys are not present in any of the other arrays.

PHP Example


        <?php  
        $array1 = array("a" => "red", "b" => "green", "c" => "blue");  
        $array2 = array("a" => "yellow", "d" => "pink");  
        print_r(array_diff_key($array1, $array2));  
        ?>
                    

Output

Array
(
[b] => green
[c] => blue
)

PHP Example


<?php  
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");  
$array2 = array("b" => "banana", "d" => "dragonfruit");  
$array3 = array("c" => "cherry", "e" => "elderberry");  
print_r(array_diff_key($array1, $array2, $array3));  
?>
                    

Output

Array
(
[a] => apple
)