PHP array function to find common elements from array

327 viewsPhp
1
0 Comments

Need array function to find common elements from the array.

$array1 = [1, 2, 3, 4, 5];
$array2 = [4, 5, 6, 7, 8];
$array3 = [4, 5, 9, 10];

 

Vivek Choudhary Answered question June 27, 2023
1

In PHP, you can use the array_intersect() function to find the common elements between two or more arrays.

This function compares the values of all arrays provided and then returns a new array that contains all of the values that are common in all of the arrays.

$commonElements = array_intersect($array1, $array2, $array3);

print_r($commonElements);

Output

Array ( [3] => 4 [4] => 5 )

Vivek Choudhary Edited answer June 28, 2023