Explain array_intersect() function in PHP.

PHPProgrammingServer Side Programming

In this article, we will learn an array_intersect() a predefined PHP array function. This builtin function of PHP is utilized to figure the convergence of at least two arrays. The function is utilized to compare at least two arrays and returns the matches. The function prints just those components of the first array that are available in all different array.


 Live Demo

   $array1 = array(5, 10, 15, 20,34);
   $array2 = array(20, 10, 15, 55, 100);
   $intersect_array = array_intersect($array1,$array2);


[1] => 10
[2] => 15
[3] => 20


In the above example, we have declared two arrays and after that, we have implemented array_intersesct(), which results in an array composed of the matched elements present in both the array.


The keys of elements are preserved. That is, the keys of elements in the output array will be the same as that of the keys of those elements in the first array.

Updated on 29-Jun-2020 08:57:27