Current Location: Home> Function Categories> array_diff_key

array_diff_key

Use key name comparison to calculate the difference set of arrays
Name:array_diff_key
Category:Array
Programming Language:php
One-line Description:Compare arrays and return the difference set (compare only key names).

Definition and usage

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.).

Example

Example 1

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

Example 2

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

Example 3

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

grammar

 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.

illustrate

array_diff_key() function returns an array that includes all keys in the array being compared but not in any other parameter array.