php

PHP array_intersect_assoc() Function

Sometimes, we need to get only common values from multiple arrays that have key-value pairs in PHP. To implement this scenario, array_intersect_assoc() is used to return common keys with values from two or multiple input arrays. This function will compare both key and value.

Syntax:
array_intersect_assoc(array_input1, array_input2,…………..)

It takes input arrays as parameters.

It is necessary to have at least two arrays inside array_intersect_assoc() function.

Now, let’s explore this function by discussing several examples.

Example 1:
In this example, we created 2 arrays: Flower1 with key-values. ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ and Flower2 with key-values: ‘flower_name’=>’lotus’,’sepal’=>3,’petal’=>4,’area’=>’water’.

Afterwards, we will perform array_intersect_assoc() on these two arrays.

<?php

//create an array named Flower1 with 4 values
$Flower1=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

//create an array named Flower2 with 4 values
$Flower2=array('flower_name'=>'lotus','sepal'=>3,'petal'=>4,'area'=>'water');
 
print_r("Actual arrays:");
print_r($Flower1);
print_r($Flower2);

print("Intersection: ");
//intersection on Flower1 and Flower2
print_r(array_intersect_assoc($Flower1,$Flower2));
?>

Output:

We can see that flower_name=>lotus and area=>water are the two pairs that are common in both arrays.

Example 2:
In this example, we created 2 arrays: Flower1 with key-values. ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ and Flower2 with key-values: ‘flower_name’=>’lilly’,’sepal’=>3,’petal’=>4,’area’=>’land’.

Afterwards, we will perform array_intersect_assoc() on these two arrays.

<?php

//create an array named Flower1 with 4 values
$Flower1=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

//create an array named Flower2 with 4 values
$Flower2=array('flower_name'=>'lilly','sepal'=>3,'petal'=>4,'area'=>'land');
 
print_r("Actual arrays:");
print_r($Flower1);
print_r($Flower2);

print("Intersection: ");
//intersection on Flower1 and Flower2
print_r(array_intersect_assoc($Flower1,$Flower2));
?>

Output:

We can see that no pairs are common in both arrays. Hence, empty array is returned.

Example 3:
In this example, we created 3 arrays: Flower1 with key-values.
‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’,
Flower2 with key-values: ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ and
Flower3 with key-values: ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ and

Afterwards, we will perform array_intersect_assoc() on these three arrays.

<?php

//create an array named Flower1 with 4 values
$Flower1=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

//create an array named Flower2 with 4 values
$Flower2=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');
 
 //create an array named Flower3 with 4 values
$Flower3=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');
 
print_r("Actual arrays:");
print_r($Flower1);
print_r($Flower2);
print_r($Flower3);


//intersection on Flower1,Flower2 and Flower3
print("Intersection: ");
print_r(array_intersect_assoc($Flower1,$Flower2));
?>

Output:

We can see that ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ is common in all three arrays. Hence, they were returned.

Example 4:
In this example, we created 4 arrays: Flower1 with key-values. ‘flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’,
Flower2 with key-values:’flower_name’=>’lotus’,’sepal’=>6,’petal’=>3,’area’=>’land’,
Flower3 with key-values:’flower_name’=>’lotus’,’sepal’=>4,’petal’=>3,’area’=>’water’ and
Flower4 with key-values:’flower_name’=>’lotus’,’sepal’=>4.

Afterwards, we will perform array_intersect_assoc() on these four arrays.

<?php

//create an array named Flower1 with 4 values
$Flower1=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

//create an array named Flower2 with 4 values
$Flower2=array('flower_name'=>'lotus','sepal'=>6,'petal'=>3,'area'=>'land');
 
 //create an array named Flower3 with 4 values
$Flower3=array('flower_name'=>'lotus','sepal'=>4,'petal'=>3,'area'=>'water');

 //create an array named Flower4 with 4 values
$Flower4=array('flower_name'=>'lotus','sepal'=>4);
 
 
print_r("Actual arrays:");
print_r($Flower1);
print_r($Flower2);
print_r($Flower3);
print_r($Flower3);

//intersection on Flower1,Flower2, Flower3 and Flower4
print("Intersection: ");
print_r(array_intersect_assoc($Flower1,$Flower2,$Flower3,$Flower4));
?>

Output:

We can see that ‘flower_name’=>’lotus’ is common in all four arrays. Hence, it is returned.

Conclusion

In this PHP tutorial, we saw how to perform intersection on arrays based on key-value pairs using array_intersect_assoc(). It returns the pairs that are common in all the given arrays. We discussed four different examples to understand the concept better.

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