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...
PHP array_diff_key() function has the following syntax.
array_diff_key(array1,array2,array3...);
Parameter | Is Required | Description |
array1 | Required. | Array to compare from |
array2 | Required. | An array to compare against |
array3,... | Optional. | More arrays to compare against |
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.
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.