foreach($array as $key => $value) is useful when you need key and its value at the same time.

Below code creates an associative array with three elements, where the keys are the names "John", "Jane", and "Jim", and the values are the ages 25, 30, and 35, respectively. The code then uses a foreach loop to iterate over the elements in the array and outputs each element as a row in the HTML table. The HTML table has two columns for the index value and the value of the element.


<?php
$array = array(
    "John" => 25,
    "Jane" => 30,
    "Jim" => 35
);

echo "<table><tr><th>Index</th><th>Value</th></tr>";

foreach ($array as $key => $value) {
    echo "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";
}

echo "</table>";
?>