PHP array_intersect() Function

The array_intersect() function compares the values of two (or more) arrays, and returns the matches.

This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

ParameterDescription
array1Required. The array to compare from
array2Required. An array to compare against
array3,…Optional. More arrays to compare against

Syntax

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

Example

$array1=array("a"=>"Prince","b"=>"James","c"=>"Samuel","d"=>"Cooper");
$array2=array("e"=>"Prince","f"=>"James","g"=>"Samuel");
$result=array_intersect($array1,$array2);
print_r($result);

Output

Array ( [a] => Prince [b] => James [c] => Samuel )

Leave a Reply

Your email address will not be published. Required fields are marked *