Reputation: 15
I want to ask pretty simple question (for most of you) but I can't find the solution right now.
We have $names = array('Alex','James','Jack');
I want to use for loop to echo this:
1. Alex
2. James
3. Jack
But I'm using this loop right now which isn't working as I want it to:
for($i = 0; $i <= count($names); $i++)
{ echo $i.$names[$i]."<br/>"; }
and it's echoing this:
0. Alex
1. James
2. Jack
The problem is because we are starting from 0 because it's an array. If I put a starting point 1 it's missing the first object from the array.
What's the fix for that?
Upvotes: 1
Views: 464
Reputation: 9518
Why not just increment your variable
$names = array('Alex','James','Jack');
for($i = 0; $i <= count($names); $i++) {
$j = $i+1;
echo $j.$names[$i]."<br/>";
}
Upvotes: 3