The array_intersect() is a built-in function in PHP used for computing the intersection of two or more arrays. The function takes two or more arrays as arguments, after comparing values of arrays the function returns an array containing all values from first array which are also present in other arrays. So the resultant array contains the values which are common in all the arrays passed as arguments.
Note : array_intersect() preserved the keys.
Syntax : array_intersect(array1, array2, array3,.....)
array1 : An array which compared with other arrays.(Required)
array2 : An array which compared with array1.(Required)
array3 : Another array which compared with array1.(Optional)
Example :
<?php
$array1 = array("a" => "orange", "apple", "mango", "pear");
$array2 = array("b" => "peach", "orange", "mango", "banana");
$result = array_intersect($array1, $array2);
print_r($result);
?>
Output : Array ( [a] => orange [1] => mango )
0 Comment(s)