Level 58
Certainly! If you have an array with 12 elements and you want to iterate through it using a while loop, you can do the following:
<?php
$texts = [
'Text 1',
'Text 2',
'Text 3',
'Text 4',
'Text 5',
'Text 6',
'Text 7',
'Text 8',
'Text 9',
'Text 10',
'Text 11',
'Text 12',
];
$i = 0;
while ($i < count($texts)) {
echo $texts[$i] . "<br>";
$i++;
}
Explanation:
- Use
$i = 0to start at the first element (arrays are zero-indexed). - The loop condition should be
$i < count($texts)to avoid going out of bounds. - Inside the loop, access each element with
$texts[$i]. - Increment
$iwith$i++after each iteration.
Let me know if you need further clarification!