The foreach loop has a slightly different structure when using non-numerically indexed arrays
Example of foreach loop
foreach ($prices as $key => $value) {
echo $key." – ".$value."<br />";
}
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
<?php
$season = array ("Summer", "Winter", "Autumn", "Rainy");
foreach ($season as $element) {
echo "$element";
echo "</br>";
}
?>
Multi-dimensional array foreach Example
<?php
$a = array();
$a[0][0] = "Alex";
$a[0][1] = "Bob";
$a[1][0] = "Camila";
$a[1][1] = "Denial";
foreach ($a as $e1) {
foreach ($e1 as $e2) {
echo "$e2\n";
}
}
?>