Computer >> Computer tutorials >  >> Programming >> PHP

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.