PHP array_diff_key() Function
Complete PHP Array Reference Manual
Example
Compare two arrays by theirkeys, and return the difference:
" red " , " b " => " green " , " c " => " blue " ) ; $a2 = array ( " a " => " red " , " c " => " blue " , " d " => " pink " ) ; $result = array_diff_key ( $a1 , $a2 ) ; print_r ( $result ) ; ?>Run Example »
Definition and Usage
The array_diff_key() function compares two (or more) arrays by theirkeys , and returns the difference.
This function compares the keys of two (or more) arrays and returns a difference array containing all the keys in the compared array (array1) that are not in any of the other argument arrays (array2 or array3 , etc.).
Syntax
array_diff_key(
array1,array2,array3...
);
| Parameters | Description |
|---|---|
| array1 | Required. The first array to compare with the other arrays. |
| array2 | Required. An array to compare with the first array. |
| array3,... | Optional. Other arrays to compare with the first array. |
Technical Details
| Return Value: | Returns a difference array containing all the keys and values in the compared array (array1) that are not in any of the other argument arrays (array2 or array3 , etc.). |
|---|---|
| PHP Version: | 5.1+ |
More examples
Example 1
Compare two numeric arrays by theirkeys, and return the difference:
Run Example »
Example 2
Compare three arrays by theirkeys, and return the difference:
" 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 ) ; ?>Run Example »
Complete PHP Array Reference Manual