php

PHP array_diff_key() Function

array_diff_key() function in PHP is used to return the elements with key-value pairs present in the first input array but not present in the remaining input arrays(second array, third array, etc.) based on keys. It compared only the keys.

Simply, we can say that it returns the difference of arrays that have key-value pairs. It is the same as the set difference operation.

Syntax:
array_diff_key(array_input1,array_input2,……….)
It takes two or more arrays as a parameter.

Example 1:
In this example, we will create three arrays (key:value pairs) and apply array_diff() to return the array difference.

Here, the first array has five elements: -1=>’lotus’,2=>’land’,3=>45,4=>14,5=>’india’,
The second array has five elements: – 1=>’jasmine’,2=>’water’,3=>25,4=>24,5=>’USA” and
The third array has five elements: -1=>’lotus’,2=>’land’,3=>45,4=>14,5=>’india’.

<?php

//create an array named Flower1
$Flower1=array(1=>'lotus',2=>'land',3=>45,4=>14,5=>'india');
 
 //create an array named Flower2
$Flower2=array(1=>'jasmine',2=>'water',3=>25,4=>24,5=>'USA');

//create an array named Flower3
$Flower3=array(1=>'lotus',2=>'land',3=>45,4=>14,5=>'india');

print_r($Flower1);
print_r($Flower2);
print_r($Flower3);

print("Array Difference: ");
//array_diff on $Flower1, $Flower2 and $Flower3
print_r(array_diff_key($Flower1,$Flower2,$Flower3));
?>

Output:

We can see that keys – 1,2,3,4,5 are present in the first array and also in the third array. Hence, no element is returned.

As a result, an empty array (Array()) is returned.

Example 2:
In this example, we will create three arrays (key:value pairs) and apply array_diff_key() to return the array difference.

Here, the first array has five keys: -1,2,3,4,5
The second array has five keys: – 6,7,8,9,10 and
The third array has five keys: – 11,12,13,14,and 15.

<?php

//create an array named Flower1
$Flower1=array(1=>’marigold’,2=>’both’,3=>5,4=>1,5=>’nepal’);
 
 //create an array named Flower2
$Flower2=array(6=>’jasmine’,7=>’water’,8=>25,9=>24,10=>’USA’);

//create an array named Flower3
$Flower3=array(11=>’lotus’,12=>’land’,13=>45,14=>14,15=>’india’);

print_r($Flower1);
print_r($Flower2);
print_r($Flower3);

print(“Array Difference:);
//array_diff on $Flower1, $Flower2 and $Flower3
print_r(array_diff_key($Flower1,$Flower2,$Flower3));
?>

Output:

We can see that keys -1,2,3,4,5 are present in the first array and are not present in the remaining two arrays. Hence, all elements from the first array are returned.

Example 3:
In this example, we will create two arrays (key:value pairs) and apply array_diff_key() to return the array difference.

Here, the first array has five keys: -1,2,3,4,5 and
The second array has five keys: – 6,7,8,9,10.

<?php

//create an array named Flower1
$Flower1=array(1=>'lotus',2=>'land',3=>45,4=>14,5=>'india');
 
 //create an array named Flower2
$Flower2=array(6=>'jasmine',7=>'water',8=>25,9=>24,10=>'USA');


print_r($Flower1);
print_r($Flower2);


print("Array Difference: ");
//array_diff on $Flower1 and  $Flower2
print_r(array_diff_key($Flower1,$Flower2));
?>

Output:

We can see that key-values -1,2,3,4,5 are present in the first array and not in the second array. Hence, all elements from the first array are returned.

Example 4:
In this example, we will create two arrays (key:value pairs) and apply array_diff_key() to return the array difference.

Here, the first array has five keys: -1,2,3,4,5 and
The second array has five keys: – 1,2,7,8,3.

<?php

//create an array named Flower1
$Flower1=array(1=>'lotus',2=>'land',3=>45,4=>14,5=>'india');
 
 //create an array named Flower2
$Flower2=array(1=>'jasmine',2=>'both',7=>25,8=>24,3=>'india');


print_r($Flower1);
print_r($Flower2);


print("Array Difference: ");
//array_diff on $Flower1 and  $Flower2
print_r(array_diff_key($Flower1,$Flower2));
?>

Output:

We can see that keys 4 and 5 are present in the first array but not in the second array. Hence, these elements from the first array are returned.

Conclusion

In this PHP article, we saw how to perform array_diff_key() on two or multiple arrays with key_value pairs. Based on the key, it returns the elements present in the first array but not in the remaining arrays. This concept was discussed with four different examples.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain