PHP | array_intersect() Function
This builtin function of PHP is used to compute the intersection of two or more arrays. The function is used to compare the values of two or more arrays and returns the matches. The function prints only those elements of the first array that are present in all other arrays.
Syntax:
array array_intersect($array1, $array2, $array3, $array4...)
Parameters: The array_intersect() function takes at least two arrays as arguments. It can take any number of arrays greater than or equal to two separated by commas (‘,’).
Return Type: The function returns another array containing the elements of the first array that are present in all other arrays passed as the parameter. If no element matches then, a NULL array is returned.
Note: The keys of elements are preserved. That is, the keys of elements in output array will be same as that of keys of those elements in the first array.
Examples:
Input : $array1 = array(5, 10, 15, 20, 25, 30)
$array2 = array(20, 10, 15, 55, 110, 30)
$array3 = array(10, 15, 30, 55, 100, 95)
Output :
Array
(
[1] => 10
[2] => 15
[5] => 30
)
Input : $array1 = array("ram", "laxman", "rishi", "ayush");
$array2 = array("ayush", "gaurav", "rishi", "rohan");
$array3 = array("rishi", "gaurav", "ayush", "ravi");
Output :
Array
(
[2] => rishi
[3] => ayush
)
Below program illustrates the array_intersect() function in PHP:
<?php // PHP function to illustrate the use of array_intersect() function Intersect($array1, $array2, $array3) { $result = array_intersect($array1, $array2, $array3); return($result); } $array1 = array(5, 10, 15, 20, 25, 30); $array2 = array(20, 10, 15, 55, 100, 110, 30); $array3 = array(10, 15, 30, 55, 100, 95); print_r(Intersect($array1, $array2, $array3)); ?> |
Output:
Array
(
[1] => 10
[2] => 15
[5] => 30
)
Reference: http://php.net/manual/en/function.array-intersect.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.map.set() Function
- PHP | max( ) Function
- PHP | exp() Function
- D3.js | d3.min() function
- PHP | Ds\Map get() Function
- CSS | rgb() Function
- PHP | min( ) Function
- p5.js | box() Function
- p5.js | value() Function
- D3.js | d3.rgb() Function
- p5.js | int() function
- D3.js | d3.hcl() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



