In this tutorial, you shall learn about PHP array_diff_key() function which can compute the difference of an array against other arrays based on keys, with syntax and examples.

PHP array_diff_key() Function

PHP array_diff_key() function computes the difference of an array against other arrays based on keys. Only key/index of the arrays is used for comparison.

In this tutorial, we will learn the syntax of array_diff_key(), and how to use this function to find the difference of an array from other arrays based on keys, covering different scenarios based on array type and arguments.

Syntax of array_diff_key()

The syntax of PHP array_diff_key() function is

array_diff_key ( array $array1 , array $array2 [, array $... ] ) : array

where

ParameterDescription
array1[mandatory] Array of interest. Compare the keys/index of this array against other arrays’.
array2[mandatory] Array of reference. The keys of this array are used to comparison against.

Along with array2, you can provide as many number of arrays to compare against. But these additional arrays are optional.

Return Value

The array_diff_key() function returns an array of elements whose keys are present in $array1 but not present in $array2 or other arrays(if provided).

ADVERTISEMENT

Examples

1. Compute difference of Arrays based on keys: array1 – array2

In this example, we will take an associative array, array1, with key-value pairs, compare it against another array array2, and find their difference using array_diff_key() function.

PHP Program

<?php
$array1 = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');
$array2 = array('a'=>'apricot', 'c'=>'cherry');
$result = array_diff_key($array1, $array2);
print_r($result);
?>

Output

The keys 'a' and 'c' of array1 are present in array2. But key 'b' of array1 is not present in array2. array_diff_key() returns array with key-value pairs, whose keys are present in array1, but not in array2. Therefore, the resulting array has key-value pair with key 'b' only.

'b'=>'banana' and 'b1'=>'banana' differ in the key part.

2. Compute difference of Array against Multiple Arrays

In this example, we will take an array: array1 and find the difference of this array against two arrays: array2 and array3.

PHP Program

<?php
$array1 = array('a'=>'apple', 'b'=>'banana', 'c'=>'cherry');
$array2 = array('a'=>'apricot');
$array3 = array('c'=>'cranberry');
$result = array_diff_key($array1, $array2, $array3);
print_r($result);
?>

Output

The key 'a' is present in array2, and the key 'c' is present in array2. Rest of the keys of array1 are not present in any of the arrays [array2, array3]. So, those key-value pairs of array1, that are not present in other arrays are returned in the resulting array.

3. Compute Difference of Indexed Arrays based on Index

In this example, we will take indexed arrays and find their difference. array_diff_key() will consider only key or index to compute difference. In our previous examples, we have take associative arrays, so key is considered in those programs. But here, we are taking indexed arrays. So, index will be considered while computing difference.

For a better understanding, we have printed out the input arrays as well with index representation.

PHP Program

<?php
$array1 = array(21, 4, 87);
$array2 = array(21, 87);
$result = array_diff_key($array1, $array2);
print_r($array1);
echo "<br>";
print_r($array2);
echo "<br>";
print_r($result);
?>

Output

Values with index 0 and 1 are present in both the arrays. But value with index 2, is present in array1, but not in array2. Therefore, value with index 2 will be the difference computed by array_diff_key().

Conclusion

In this PHP Tutorial, we learned how to compute the difference of arrays considering only key/index, using PHP Array array_diff_key() function.