So, i have 2 arrays which look like this:
JavaScript
x
$a = array(1,3,5);
$b = array(2,3,4,5);
The expected result should look like this:
JavaScript
array(3,5);
Is there a quick and easy way to achieve my expected result? 🙂
Advertisement
Answer
Option One:
JavaScript
$a = array(1,3,5);
$b = array(2,3,4,5);
$result = array_intersect($a, $b);
print_r($result);
Option 1 output:
JavaScript
Array
(
[1] => 3
[2] => 5
)
Option 2:
JavaScript
$a = array(1,3,5);
$b = array(2,3,4,5);
$resultTwo = [];
foreach($a as $val){
if(in_array($val, $b)){
$resultTwo[] = $val;
}
}
print_r($resultTwo);
Option 2 Output (unlike option 1, the array index starts from 0):
JavaScript
Array
(
[0] => 3
[1] => 5
)