![]() |
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 FunctionsPHP provides built-in functions array_merge() and array_intersect() which can be used to find the union and intersection of arrays, respectively.
Example: The below mentioned code calculates the Union & Intersection of two unsorted array in PHP using above mentioned approach.
Output Union: 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.
Example: The below mentioned code calculates the Union & Intersection of two unsorted array in PHP using above mentioned approach.
Output Union: Array ( [0] => 3 [1] => 5 [2] => 8 [3] => 10 [4] => 15 [5] => 20 ) Intersection: Array ( [0] => 5 [1] => 10 ) |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |