PHP array_intersect()
Published Aug 12, 2023
Contribute to Docs
The array_intersect() function compares the values in all arrays provided against the first array and returns the matching values.
Syntax
array_intersect(first_array, second_array, third_array...)
array_intersect() takes two or more arrays and returns any values from first_array that are also present in second_array and third_array, and any other array that is given.
Example
<?php$first_array=array(5, 2, 1, 12, 65, 7);$second_array=array(1, 5, 3, 7, 2, 6);$third_array=array(7, 6, 4, 2, 7, 65, 100);$result=array_intersect($first_array, $second_array, $third_array);print_r($result);?>
The above example will produce the following output:
Array ([1] => 2 [5] => 7)
Note: Only the values from
$first_arraypresent in all other arrays are returned. In the example above,2and7appear in all three arrays so they are returned as matches. If no matches are found, the function returns an empty array.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours