PHP array_intersect_key Function | Intersection of Arrays

PHP array_intersect_key is an inbuilt function in PHP. It is used to compute the intersection of two arrays. It returns an array which contains elements by comparing only keys present in all the input arrays. On the other hand, the PHP array_intersect Function compares only array values and not the keys. In this article, we will discuss the PHP array_intersect_key Function. Also, we will discuss a few examples of using it.

Syntax

array array_intersect_key($array1, $array2, … $arrayN);

Parameters

The PHP array_intersect_key Function expects atleast two parameters. However, you can pass any number of parameters to the function. The description of the parameters is as follows:

  • $array1: The first array refers to the master array. It contains the elements to check. Also, it is mandatory to pass it.
  • $array2: The second array is mandatory and it refers to the array which the function will compare against.
  • $arrayN: You can pass N number of arrays separating them by comma as multiple optional parameters are allowed in the function.

Return Value

The PHP array_intersect_key Function returns an array containing elements which are present in all the input arrays by matching only keys.

Note: Two elements are equal only if their (string) $element1 === (string) $element2. In other words, when their string representation are same.

PHP array_intersect_key Function Diagram

Examples

Let’s discuss a few examples to demonstrate the use of array_intersect_key Function.

Example 1: Simple Two Arrays

Let’s consider two simple integer arrays and find their intersection on keys.

<?php
	
	$array1 = array(0 => 1, 1 => 2, 2 => 3, 3 => 4);
	$array2 = array(0 => 2, 1 => 2, 2 => 1, 10 => 4);
	$intersect = array_intersect_key($array1, $array2);
	print_r($intersect);
	/*	
	Array
	(
	    [0] => 1
	    [1] => 2
	    [2] => 3
	)
	*/

?>

In the above example, the function returns 1, 2 and 3 as the three values have the same keys. All three keys exist in both the arrays. However, the value 4 is also present in both the arrays but in different keys. Also, notice that the value in key 2 is 3 and 1 respectively, but the function preserves the value from the first array.

Example 2: String Keys

Let’s first find out the array_intersect of the two arrays:

<?php

	$array1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat');
	$array2 = array('a' => 'apple', 'z' => 'cat', 'b' => 'ball');
	$intersect = array_intersect($array1, $array2);
	print_r($intersect);
	/*
	Array
	(
	    [a] => apple
	    [b] => banana
	)
	*/
?>

The array_intersect Function returns apple and cat since they are present in both the arrays. It does not check for keys of the elements.

Now, notice the output of PHP array_intersect_key Function.

<?php

	$array1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat');
	$array2 = array('a' => 'apple', 'z' => 'cat', 'b' => 'ball');
	$intersect = array_intersect_key($array1, $array2);
	print_r($intersect);
	/*
	Array
	(
	    [a] => apple
	    [b] => banana
	)
	*/
?>

The function returns both apple and banana because the key a and b is common in both the arrays. Also, it does not preserve the value ball as the values are preserved from the first array only.

Example 3: Multiple Arrays

Similarly, we can pass multiple arrays in array_intersect_key Function.

<?php

	$array1 = array('a' => 'apple', 'b' => 'banana', 'c' => 'cat', 10, 20);
	$array2 = array('a' => 'aeroplane', 'z' => 'cat');
	$array3 = array(10, 'a'=>'ant', 4);
	$intersect = array_intersect_key($array1, $array2, $array3);
	print_r($intersect);
	/*
	Array
	(
	    [a] => apple
	)
	*/

?>

The function only returns apple (with key a) as the key a is present in all the arrays.

Conclusion

In conclusion, we discussed the array_intersect_assoc Function. It returns an array containing elements which are present in all the input arrays. You can read more about it on PHP Official Documentation. You can also learn about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

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