Home >>PHP Array Functions >PHP array_intersect() Function
PHP array_intersect() Function is used to calculate the intersection of two or more arrays. It is used to compare the values of two or more given input arrays and returns the matches. It accepts atleast two arrays as arguments. It returns another array containing the elements of the first array that are present in all other arrays.
Syntax:
array_intersect($array1, $array2, $array3, ...);
Parameter | Description |
---|---|
array1 | This is a required parameter. This parameter definesthe array to compare from. |
array2 | This is a required parameter. This parameter definesan array to compare against. |
array3,... | This is an optional parameter. This parameter definesmore arrays to compare against. |
Here is an example of array_intersect() function in PHP:
<html> <body> <pre> <?php $x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4","e"=>"5"); $y=array("e"=>"1","f"=>"2","g"=>"4"); $result=array_intersect($x,$y); print_r($result); ?> </pre> </body> </html>
Array ( [a] => 1 [b] => 2 [d] => 4 )
Example 2:
<html> <body> <pre> <?php $x=array("1","2","3","4","5","6","7","8","9"); $y=array("1","2","4","7","11"); $result=array_intersect($x,$y); print_r($result); ?> </pre> </body> </html>
Array ( [0] => 1 [1] => 2 [3] => 4 [6] => 7 )