PHP array_diff_key function

PHP array_diff_assoc() function
PHP array_diff_uassoc() function

In PHP, the array_diff_key() method compares two or more arrays and returns an array containing the differences depending on the keys. This method is useful for comparing arrays and determining which keys are present in one array but not in others.

The array diff key() method has the following syntax:

What is the syntax of the array_diff_key function in PHP?

array array_diff_key ( array $array1 , array $array2 [, array $... ] )
ParametersDescription
array1The first array to compare with other arrays – required
array2Second array to make a comparison against- required
array3,…Further arrays to compare against – optional
array_diff_key function in PHP

The first array to be compared is specified by the $array1 argument, while the second array is specified by the $array2 parameter. Additional arrays can be passed as arguments and are compared to $array1.

To understand how the array diff key() method works, consider the following examples:

Example 1:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$diff = array_diff_key($array1, $array2);
print_r($diff);

Output:

Array
(
    [green] => banana
    [blue] => cherry
)

$array1 and $array2 are compared in this example, and the $diff array includes the keys that are present in $array1 but not in $array2.

Example 2:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$array3 = array("green" => "kiwi", "pink" => "grapefruit");
$diff = array_diff_key($array1, $array2, $array3);
print_r($diff);

Output:

Array
(
    [blue] => cherry
)

$array1, $array2, and $array3 are compared in this example, and the $diff array includes the keys that are present in $array1 but not in $array2 or $array3.

Example 3:

$array1 = array("red" => "apple", "green" => "banana", "blue" => "cherry");
$array2 = array("red" => "apple", "yellow" => "lemon");
$array3 = array("green" => "banana", "pink" => "grapefruit");
$diff = array_diff_key($array1, $array2, $array3);
print_r($diff);

Output:

Array
(
    [blue] => cherry
)

Because the keys “green” are included in all three arrays in this example, the $diff array does not contain this key.

Finally, the array diff key() method in PHP is a valuable tool for comparing arrays and determining differences depending on the keys. This method may be used to compare two or more arrays and return an array containing the keys that are present in one but not the others. The syntax is simple, and the function is simple to use. The examples in this article will help you understand how the array diff key() method works and how it may be utilized in real-world applications.

It is vital to notice that the array diff key() method only compares the keys and not the values. If you need to compare both the keys and the values, use the array diff() method instead. It’s also worth noting that the order of the arrays being compared is unimportant.

Overall, the array diff key() method is useful for comparing arrays and determining differences depending on the keys. It’s a basic yet powerful tool that every PHP coder should be familiar with.

PHP array_diff_assoc() function
PHP array_diff_uassoc() function
en English
X
Scroll to Top