Intersect of two arrays are quite simple, intersection is the term that is used to get the same elements present in two arrays is to be called as Intersection. This task is very easy if we use a function and can be done in seconds. But we are going to do this task without using any function. We will only use variables, and loops to do this task. Further you can learn about the intersection from this link. Click here
Intersect Steps
- First we need to initialize two arrays and then assign some values in it.
- Make sure you take some same values in both arrays.
- Then we will count the size of array for that we will use foreach loop to get the size of an array.
- After that we will run a for loop with this counted size less than and equal to that one.
- And then we use a if condition it will check either a particular array element is set or not.
- If it will be set then again it will go in another for loop condition that will run to compare current array to the other array so that we can dumb the value if it is equal to any of value in the other array.
Code Example
<center> <?php ini_set('display_errors', 0); error_reporting(E_ERROR | E_WARNING | E_PARSE); ?> <?php $array_one = array (23,34,78,12,10,56,40,20,45,43,76,87); $array_two = array (23,50,23,43,34,64,26,41,47,76,87,43); echo "<pre>"; echo print_r($array_one); echo "<br>"; echo print_r ($array_two); foreach ($array_one as $key=>$value) { $b = $b + 1; } echo "<br>"; for ($count=0 ; $count<=$b ; $count++) { if (isset($array_one[$count])) { for ($i=0 ; $i<=$b ; $i++) { if($array_one[$count] == $array_two[$i]) { $abd = $array_one[$count]; $array_three .= $abd.","; break; } } } } echo "<h3>"."Same Values Are ".$array_three."</h3>"; ?></center>
Output
Array ( [0] => 23 [1] => 34 [2] => 78 [3] => 12 [4] => 10 [5] => 56 [6] => 40 [7] => 20 [8] => 45 [9] => 43 [10] => 76 [11] => 87 ) 1 Array ( [0] => 23 [1] => 50 [2] => 23 [3] => 43 [4] => 34 [5] => 64 [6] => 26 [7] => 41 [8] => 47 [9] => 76 [10] => 87 [11] => 43 ) 1 Same Values Are 23,34,43,76,87,