Say I have an array similar to the following and I’m looping through it as such:
$sidebar = array("Best of the Month" => $featuredBestMonth,
"Featured Content" => $featuredContent);
<? while($item = current($sidebar)):?>
<? if($item):?>
<h3><?=key($sidebar);?></h3>
<? foreach($item as $single):?>
<p><?=$single['title'];?></p>
<? endforeach;?>
<? endif;?>
<? next($sidebar);?>
<? endwhile;?>
How can I count the current array number, so the first while would display 1 and the second would display 2?
I know I could do it with $i++; but just wondered if there was an array function to do just this?
Not sure if I can use key with a foreach loop?
Oi – all those tags (and short tags at that) are painful to look at. Feels a lot like PHP 4, anyone forced to support this code isn’t going to be very happy. No offense intended, but can I suggest something like:
I’m still not sure this code makes sense, but based on your example it should at least produce the same result (also not sure where you want your counter to be displayed, since your question isn’t very clear..so I guessed).
Good luck.