Loop Through an Indexed Array

You can use for loop to get the indexed array result

Example

[php]<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>[/php]

In the above code, we are telling php script to first count the array length. We have defined the variable $arrlenght to array length. In the next statement, we are using for loop code. In that, we have disclosed the $x value to 0. The $x is stated it is lower than the $arrlength count and in the condition we are asking it increments it by one. Then we are running the echo statement to get the value.

The above code will show the result.

Volvo
BMW
Toyota

Related Posts

Leave a Reply