

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Questions & Answers
- PHP foreach Loop.
- Using foreach loop in arrays in C#
- Multiple index variables in PHP foreach loop
- How to count values from a PHP array and show value only once in a foreach loop?
- Stripping last comma from a foreach loop in PHP?
- PHP Casting Variable as Object type in foreach Loop
- foreach Loop in C#
- How to iterate two Lists or Arrays with one foreach statement in C#?
- Parsing JSON array with PHP foreach
- The internal working of the ‘foreach’ loop in PHP
- Merge two arrays with alternating Values in JavaScript
- MongoDB query to find matching documents given an array with values?
- How to know if two arrays have the same values in JavaScript?
- Difference Between For and Foreach in PHP
- Iterating C# StringBuilder in a foreach loop
Advertisements