PHP Find Union & Intersection of Two Unsorted Arrays Last Updated : 20 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 the intersection includes only the elements that are present in both arrays. Below are the approaches to find the union and intersection of two unsorted array using PHP: Table of Content Using Built-in FunctionsUsing Loops and Conditional StatementsUsing Built-in FunctionsPHP provides built-in functions array_merge() and array_intersect() which can be used to find the union and intersection of arrays, respectively. array_merge($arr1, $arr2) merges the two arrays into one.array_unique($arr1) removes duplicate values from the array to form the union.array_intersect($arr1, $arr2) finds the intersection of the two arrays.Example: The below mentioned code calculates the Union & Intersection of two unsorted array in PHP using above mentioned approach. PHP <?php // Define two unsorted arrays $arr1 = [3, 5, 8, 10]; $arr2 = [5, 10, 15, 20]; // Find the union of the arrays $union = array_unique(array_merge($arr1, $arr2)); // Find the intersection of the arrays $intersection = array_intersect($arr1, $arr2); echo "Union: "; print_r($union); echo "\nIntersection: "; print_r($intersection); ?> OutputUnion: Array ( [0] => 3 [1] => 5 [2] => 8 [3] => 10 [6] => 15 [7] => 20 ) Intersection: Array ( [1] => 5 [3] => 10 ) Using Loops and Conditional StatementsYou can also find the union and intersection of two arrays manually using loops and conditional statements. First we adds all elements of $arr1 to the $union array.Then, for each element of $arr2:If the element is not already in $union, it is added to form the union.If the element is also in $arr1, it is added to $intersection to form the intersection.Example: The below mentioned code calculates the Union & Intersection of two unsorted array in PHP using above mentioned approach. PHP <?php // Define two unsorted arrays $arr1 = [3, 5, 8, 10]; $arr2 = [5, 10, 15, 20]; // Initialize empty arrays for // union and intersection $union = []; $intersection = []; // Add all elements of the first // array to the union foreach ($arr1 as $element) { $union[] = $element; } // Iterate through the second array foreach ($arr2 as $element) { // Add to the union if not already present if (!in_array($element, $union)) { $union[] = $element; } // Add to the intersection if present // in the first array if (in_array($element, $arr1)) { $intersection[] = $element; } } // Display the results echo "Union: "; print_r($union); echo "\nIntersection: "; print_r($intersection); ?> OutputUnion: Array ( [0] => 3 [1] => 5 [2] => 8 [3] => 10 [4] => 15 [5] => 20 ) Intersection: Array ( [0] => 5 [1] => 10 ) Comment More infoAdvertise with us Next Article PHP Find Union & Intersection of Two Unsorted Arrays A ashokjaiswal Follow Improve Article Tags : PHP Similar Reads 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 | Find Intersection of two arrays 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 min read PHP array_intersect_uassoc() Function The array_intersect_uassoc() function is an inbuilt function in PHP. It is used to compare key and values of two or more arrays by using a user-defined comparison function and return the matches.The comparison function returns an integer equal to, greater than, or less than zero. If the first argume 6 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 PHP array_unique() Function The PHP array_unique() function removes duplicate values from an array, preserving the first occurrence of each value and reindexing the array with keys preserved. It's useful for filtering out redundant data and ensuring a collection of unique elements within an array. Syntaxarray array_unique($arr 2 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 PHP array_uintersect() Function The array_uintersect() is an inbuilt function in PHP and is used to compute the intersection of two or more arrays depending on the values. The first array values are compared with all the other arrays with the help of an user-defined function and the matches are returned. Syntax: array_uintersect($ 2 min read PHP array_uintersect_uassoc() Function The array_uintersect_uassoc() function is an inbuilt function in PHP which is used to computes the intersection of two arrays. There is the role of a callback function which helps in comparing and computing the indexes values, It compares the keys. It also compares the values inside the two or more 2 min read How to get total number of elements used in array in PHP ? In this article, we will discuss how to get total number of elements in PHP from an array. We can get total number of elements in an array by using count() and sizeof() functions. Using count() Function: The count() function is used to get the total number of elements in an array. Syntax: count(arra 2 min read Like