array_diff_key
Use key name comparison to calculate the difference set of arrays
array_diff_key()
function is used to compare the key names of two (or more) arrays and return the difference set.
The function compares the key names of two (or more) arrays and returns an array of differences that include all key names in the array being compared ( array1 ) but not in any other parameter array ( array2 or array3 , etc.).
Compare the key names of two arrays and return the difference:
<?php $a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ; $a2 = array ( "a" => "red" , "c" => "blue" , "d" => "pink" ) ; $result = array_diff_key ( $a1 , $a2 ) ; print_r ( $result ) ; ?>
Try it yourself
Compare the key names of two numeric arrays and return the difference:
<?php $a1 = array ( "red" , "green" , "blue" , "yellow" ) ; $a2 = array ( "red" , "green" , "blue" ) ; $result = array_diff_key ( $a1 , $a2 ) ; print_r ( $result ) ; ?>
Try it yourself
Compare the key names of three arrays and return the difference:
<?php $a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ; $a2 = array ( "c" => "yellow" , "d" => "black" , "e" => "brown" ) ; $a3 = array ( "f" => "green" , "c" => "purple" , "g" => "red" ) ; $result = array_diff_key ( $a1 , $a2 , $a3 ) ; print_r ( $result ) ; ?>
Try it yourself
array_diff_key ( array1 , array2 , array3 ... ) ;
parameter | describe |
---|---|
array1 | Required. The first array that is compared with other arrays. |
array2 | Required. The array that compares to the first array. |
array3 ,... | Optional. Other arrays that are compared with the first array. |
array_diff_key()
function returns an array that includes all keys in the array being compared but not in any other parameter array.