Example 1 — Modern Replacement: foreach
Use foreach to read every key and value. This works in all current PHP versions.
<?php
$fruits = ["a" => "apple", "b" => "banana"];
foreach ($fruits as $key => $value) {
echo $key, " => ", $value, PHP_EOL;
} How It Works
- Create an array with two keyed items.
- foreach visits each item in order.
- $key holds the key and $value holds the value for that pass.
- Each pair is printed on its own line.

