PHP | Find Intersection of two arrays Last Updated : 27 Mar, 2018 Comments Improve Suggest changes Like Article Like Report You are given two arrays of n-elements each. You have to find all the common elements of both elements, without using any loop in php and print the resulting array of common elements. Example: Input : array1[] = {3, 5, 2, 7, 9}, array2[] = {4, 3, 2, 7, 8} Output : array ( [0] => 3, [1] => 2, [2] => 7) Input : array1[] = {3, 5, 7}, array2[] = {2, 4, 6} Output : array ( ) In C/Java, we have to traverse one of the array and for each element you have to check its presence in second array. But PHP provides an inbuilt function (array_intersect()) which returns the common elements (intersect) of two array. array_intersect($array1, $array2) : Returns an array containing all the values of array1 that are present in array2. Note that keys are preserved. Note : As array_intersect() returns array with preserved keys, we will use array_values() which will re-order the keys. // find intersect of both array $result = array_intersect($array1, $array2); // re-order keys $result = array_values($result); // print resultant array print_r($result); PHP <?php // declare arrays $array1 = array(2, 5, 7, 6, 9); $array2 = array(3, 2, 5, 6, 8); // find intersect of both array $result = array_intersect($array1, $array2); // re-order keys $result = array_values($result); // print resultant array print_r($result); ?> Output: Array ( [0] => 2 [1] => 5 [2] => 6 ) Comment More infoAdvertise with us Next Article PHP | Find Intersection of two arrays A Abhinav96 Follow Improve Article Tags : Web Technologies PHP Similar Reads PHP Find Union & Intersection of Two Unsorted Arrays Given two unsorted arrays, i.e. arr1, and arr2, the task is to find the union and intersection of two unsorted arrays in PHP. Union and intersection of two arrays are common operations in data manipulation and analysis. The union of two arrays includes all the unique elements from both arrays, while 3 min read 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_inter 2 min read PHP array_intersect_key() Function This builtin function of PHP is used to compute the intersection of two or more arrays. The function is different from array_intersect() and array_intersect_assoc() in a way that it uses the keys for the comparison and returns the matching key elements. The function prints only those elements of the 2 min read How to Find Union and Intersection of Two Unsorted Arrays in PHP? Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol 4 min read PHP | array_intersect_ukey() Function The array_intersect_ukey() function is an inbuilt function in PHP which is used to compute the intersection of two or more array against keys with the help of user-defined function and the function return an array. The returned array is the first array, which is matching keys and present in all para 5 min read Like