In this tutorial, you shall learn how to iterate through an array in PHP using For loop, with the help of example programs.
PHP – Iterate through an array using For Loop
To iterate through the elements of an array, we can use For loop. We initialize an index variable with 0, increment it during each loop iteration, and access the elements of the array using this index inside For loop.
The syntax to iterate through an array arr
using For loop is
for ($i = 0; $i < count($array); $i++) { //$array[$i] }
Examples
1. Iterate through an array using For Loop
In this example, we will take an array arr
with some elements, and iterate through the elements of the array arr
using For Loop.
PHP Program
<?php $arr = ["apple", "banana", "cherry"]; for ($index = 0; $index < count($arr); $index++) { echo $arr[$index] . "<br>"; } ?>
Output
Conclusion
In this PHP Tutorial, we learned how to iterate through elements in an array using For Loop.