PHP Tutorial - PHP array_diff_key() function compare the keys of two arrays, and return the differences






Definition

The array_diff_key() function compares the keys of two or more arrays, and returns entries from array1 that are not present in array2 or array3...

Syntax

PHP array_diff_key() function has the following syntax.

array_diff_key(array1,array2,array3...);

Parameter

ParameterIs RequiredDescription
array1Required.Array to compare from
array2Required.An array to compare against
array3,...Optional.More arrays to compare against




Example

Compare the keys of two indexed arrays, and return the differences:


<?php
$a1=array("red","green","blue","java2s.com");
$a2=array("red","green","blue");

$result=array_diff_key($a1,$a2);
print_r($result);
?>

The code above generates the following result.





Example 2

Compare the keys of two associative arrays, and return the differences:


<?php
$a1=array("a"=>"A","b"=>"Bed","c"=>"Cat");
$a2=array("a"=>"A","c"=>"Cat","d"=>"Dog","j"=>"java2s.com");

$result=array_diff_key($a1,$a2);
print_r($result);
?>

The code above generates the following result.