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:

View the example in the browser
Practice here online :
See also
Previous:array_intersect_ ukey
Next: array_key_exists
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join