Looping Through Arrays in PHP
You can loop through arrays using foreach
, for
, or while
loops.
Example of Looping Through Arrays
<?php
$fruits = array("apple", "banana", "orange");
// Using foreach loop
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
// Using for loop
for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "\n";
}
?>
Output:
apple
banana
orange
apple
banana
orange
Explanation: This example demonstrates how to loop through an array using both foreach
and for
loops.