Explain array_intersect() function in PHP.


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.

Example

 Live Demo

<?php
   $array1 = array(5, 10, 15, 20,34);
   $array2 = array(20, 10, 15, 55, 100);
   $intersect_array = array_intersect($array1,$array2);
   print_r($intersect_array);
?>

output

Array
(
[1] => 10
[2] => 15
[3] => 20
)

Explanation

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.

Note

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

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements