Using foreach to Iterate Over Array Elements in PHP


Iterate over each element in an array with foreach for easy access to both keys and values.

Source Code

$fruits = ['apple' => 'red', 'banana' => 'yellow', 'grape' => 'purple'];
foreach ($fruits as $fruit => $color) {
    echo "$fruit is $colorn";
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments