foreach is the idiomatic way to walk arrays. Master value-only, key-value, and by-reference patterns to avoid subtle bugs.
Key-value iteration
foreach ($config as $key => $value) {
echo "{$key} = {$value}\n";
}
Modifying by reference
foreach ($items as &$item) {
$item = strtoupper($item);
}
unset($item); // break the reference
Iterating copies
foreach ($arr as $value) copies scalars by value; objects are object handles—mutations visible unless you clone.
Self-check
- Why
unset($item)after a reference loop? - Can you modify keys during foreach?