PHP Function Array_Diff_Key
PHP has some built in array functions that allow us to do things like sorting, searching, filtering and transforming the elements of an array. One of these functions is array_diff_key. The function compares the keys from an array against the keys from other arrays and then returns the list of entries from the first array that are not present in any of the other arrays.
Unlike the array_diff() function which compares the values of the different arrays, this function only considers the keys for comparison. This can be useful if you need to find out what differences exist between two arrays but are not interested in the exact values that make up the difference.
The arguments of the function are as follows:
Array1 - Array to compare against
Array2 - Array to compare against
Additional arrays can be added to this list if needed. These arrays will be compared against the first array and the list of entries from the first array that do not appear in any of the other arrays will be returned.
The key 'a' is present in both array1 and array2 but the key 'c' is not present in any of them. Using the array_diff_key() function we can create an array of the key-value pairs from the first array that are not present in the other arrays.
There are many more interesting and useful array functions in PHP that we can use to solve various problems. We will look at some of these in the next few articles.