Foreach loop with two arrays and if-condition evaluation to find matching values PHP?


Let’s say we have the following two arrays 

$firstArray=array(10,20,30,40,50);
$secondArray=array(100,80,30,40,90);

We need to find the matching i.e. the output should be 

30
40

Example

The PHP code is as follows

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$firstArray=array(10,20,30,40,50);
$secondArray=array(100,80,30,40,90);
foreach($firstArray as $f){
   foreach($secondArray as $s){
      if($f==$s){
         echo "The matching result is=",$f,"<br>";
      }
   }
}
?>
</body>
</html>

Output

This will produce the following output 

The matching result is=30
The matching result is=40

Updated on: 13-Oct-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements