PHP array_intersect_assoc Function | Intersection of Arrays

PHP array_intersect_assoc is an inbuilt function in PHP. It is used to compute intersection of two arrays. It returns an array which contains elements (both key and value) 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_assoc Function. Also, we will discuss a few examples of using it.

Syntax

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

Parameters

The PHP array_intersect_assoc 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_assoc Function returns an array containing elements which are present in all the input arrays by matching both the keys and values.

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

PHP array_intersect_assoc Function

Examples

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

Example 1: Simple Two Arrays

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

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

?>

In the above example, the function returns only 2 and 4 as both the elements have the same key and value. Although, 2 and 1 are also present in both the arrays, the function does not return them as they are present in different keys.

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');
	$intersect = array_intersect($array1, $array2);
	print_r($intersect);
	/*
	Array
	(
	    [a] => apple
	    [c] => cat
	)
	*/
?>

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_assoc Function.

<?php

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

The function only returns apple because it also compares keys of the elements.

Example 3: Multiple Arrays

Similarly, we can pass multiple arrays in array_intersect_assoc Function.

<?php

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

?>

The function returns those elements which are present in all the arrays with the same keys.

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 *