w3resource

PHP: array_intersect() function

PHP: Computes the intersection of arrays

The array_intersect() function is used to compare two or more arrays and returns an array containing all the values of the first array that are present in other arrays. In this operation, keys are preserved.

Version:

(PHP 4 and above)

Syntax:

array_diff (array1, array2, array3 ... )

Parameters:

Name Description Required /
Optional
Type
array1 Reference array. Required Array
array2 Compared with the first array. Required Array
array3.. Compared with the first array Optional Array

Return value:

An array containing all the keys and values in array1 if it's values exist in array2, array3 etc.

Value Type: Array

Example:

<?php
$array1=array("Orange" => 100, "Apple" => 200, "Banana" => 300, "Cherry" => 400);
$array2=array("Orange" => 100, "Apple" => 200, "Banana" => 300);
$result=array_intersect($array1, $array2);
print_r($result);
?>

Output:

Array ( [Orange] => 100 [Apple] => 200 [Banana] => 300 )

Pictorial Presentation:

php function reference: array_intersect() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_intersect_ ukey
Next: array_key_exists



Follow us on Facebook and Twitter for latest update.