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

 Live Demo

<?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.

Updated on: 2020-04-09T11:01:08+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements