PHP array_diff_key Function | Array Difference on Keys

PHP array_diff_key Function is an inbuilt function in PHP which computes the difference between two or more arrays. Unlike array_diff Function, this function computes the difference taking only the keys for comparison. It compares the keys in input arrays and returns an array containing elements present in the first array and not in subsequent arrays.

In this article, we will discuss PHP array_diff_key Function. Also, we will cover a few examples to demonstrate it’s usage.

Note: The array_diff_key Function is different from array_diff and array_diff_assoc Function. The first one uses only array values for comparison. The second one uses both keys and values. However, this function only uses keys for comparison.

Syntax

array array_diff_key($array1, $array2, $array3, …, $array_n)

Parameters

The array_diff_key Function in PHP consists of 2 mandatory parameters. However, you can provide N number of parameters to the function. The description of the parameters is as follows:

  • array1: The first array to compare from. This parameter is mandatory.
  • array2: The second array to compare against. This parameter is also mandatory.
  • arrayN: Also, More arrays can be passed to compare against. Optional.

Return Value

The PHP array_diff_key Function returns an array containing elements which are present in the first array but not in the subsequent arrays. Additionally, it compares the arrays only on keys present in them.

Also, This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]).

Examples

Let’s go through a few examples to demonstrate the working of PHP array_diff_key Function.

Example 1

For instance, let’s consider a basic example using array_diff_key function.

<?php
	$array1 = array('Chandler' => 1, 'Monica' => 4, 'Ross' => 8);
	$array2 = array('Monica' => 3, 'Rachael' => 1);
	print_r(array_diff_key($array1, $array2));
	/* Array ( [Chandler] => 1 [Ross] => 8 ) */
?>

In the above example, you can observe that the key Monica is present in both the arrays. However, Chandler and Ross are not present in the second array. The function returns the keys Chandler and Ross as they are present in the first array and not in the second array.

Example 2: Multiple Arrays

Similarly, we can pass multiple arrays in the function.

<?php
	$array1 = array('Chandler' => 1, 'Monica' => 4, 'Ross' => 8, 'Phoebe' => 10);
	$array2 = array('Monica' => 3, 'Rachael' => 1, 'Joey' => 15);
	$array3 = array('Gunther' => 12);
	$array4 = array('Joey' => 1);
	print_r(array_diff_key($array1, $array2, $array3, $array4));
	/* Array ( [Chandler] => 1 [Ross] => 8 [Phoebe] => 10 ) */
?>

Conclusion

We discussed the array_diff_key Function in PHP. It is similar to the array_diff Function but compares the arrays for both keys and values. You can also go through other PHP Array Functions on Concatly.

Additionally, do visit the Official Documentation of PHP for more information on array_diff_key Function.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *