- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiple index variables in PHP foreach loop
The ‘foreach’ loop can be used to multiple index variables of two arrays. This has been shown below −
Example
<?php $FirstArray = array('aB', 'PQ', 'cd', 'pm'); $SecondArray = array('12', '34', '90', '49'); foreach($FirstArray as $index => $value) { echo $FirstArray[$index].$SecondArray[$index]; echo "<br/>"; } ?>
Output
This will produce the following output −
aB12 PQ34 cd90 pm49
Note − If there are more than 2 arrays, nested ‘foreach’ loops can be used.
Here, 2 arrays have been declared and they are being traversed using the ‘foreach’ loop. The result is that the respective index of every array is matched and the data at those indices are displayed one next to the other.
- Related Articles
- PHP foreach Loop.
- Stripping last comma from a foreach loop in PHP?
- PHP Casting Variable as Object type in foreach Loop
- The internal working of the ‘foreach’ loop in PHP
- foreach Loop in C#
- How to get the current index of an array while using forEach loop in Kotlin?
- Using foreach loop in arrays in C#
- Iterating C# StringBuilder in a foreach loop
- Foreach loop with two arrays and if-condition evaluation to find matching values PHP?
- Manipulate for loop to run code with specific variables only PHP?
- How does the Java “foreach” loop work?
- How to count values from a PHP array and show value only once in a foreach loop?
- How to use PowerShell break statement in foreach loop?
- How to use PSCustomObject in PowerShell foreach parallel loop?
- How to use the foreach loop parallelly in PowerShell?

Advertisements