PHP array_intersect Function | Compute Intersection of Arrays

PHP array_intersect is an inbuilt function in PHP. It is used to compute the intersection of two arrays. It returns an array which contains elements present in all the input arrays. In this article, we will discuss the PHP array_intersect Function. Also, we will discuss a few examples of using it.

PHP array_intersect Vein DIagram
Intersection Vein Diagram

Syntax

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

Parameters

The PHP array_intersect 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 values 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 Function returns an array containing elements which are present in all the input arrays. Also, the function preserves the keys of the first array. Hence, if multiple arrays contain the same values, the keys of the first array will be preserved.

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

PHP array_intersect Function DIagram

Examples

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

Example 1: Simple Two Arrays

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

<?php

	$array1 = array(1, 2, 3, 4, 5, 6);
	$array2 = array(4, 5, 6, 7, 8, 9);
	$intersect = array_intersect($array1, $array2);
	print_r($intersect);
	/*
	Array
	(
	    [3] => 4
	    [4] => 5
	    [5] => 6
	)
	*/

	$intersect2 = array_intersect($array2, $array1);
	print_r($intersect2);
	/*
	Array
	(
	    [0] => 4
	    [1] => 5
	    [2] => 6
	)	
	*/

?>

In the above example, we find the intersection of the two arrays with both the combinations ($array1 as first and $array2 as second and vice versa). Although, the output of both the combinations is same, note that the function preserves the keys in the first array.

Example 2: Associative Array

Let’s pass an associative array in the PHP array_intersect Function.

<?php

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

	$intersect2 = array_intersect($array2, $array1);
	print_r($intersect2);
	/*
	Array
	(
	    [a] => apple
	    [z] => cat
	)	
	*/

?>

Similarly, we can pass associative arrays. The function returns the elements which are common in both the arrays. Also, notice that the function preserves the keys in the first array. The order of the elements is very important.

Example 3: Multiple Arrays

You can pass multiple arrays in the function as follows:

<?php

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

?>

The PHP array_intersect Function returns the elements which exist in all the arrays.

Conclusion

In conclusion, we discussed the array_intersect 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 *