Php Array Intersection

What is array_intersect() Function?

Explanation

The "array_intersect()" function is used to compute the intersection of arrays.

Syntax:


array_intersect(array1,array2,array3....)

In the above syntax values and keys of "array1" is compared with other arrays, returns the values from the "array1" if it is present in all other arrays

Example :


<?php
$a = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$b = array('orange' => 5, 'blue' => 6, 'green' => 7, 'red'=> 8);
print_r(array_intersect($a, $b));
?>
Result :

Array {["blue"]=> int(1) ["green"]=> int(3)}

In the above example the two arrays "$a", "$b" are compared for keys and values, returns an array elements that are present in both the arrays

PHP Topics


Ask Questions

Ask Question