Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
<!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
Advertisements