0

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?

3
  • 1
    I'd use a foreach loop + counter. You can find a list of array functions here: php.net/manual/en/ref.array.php Commented May 10, 2012 at 18:35
  • Is that code right? Because it looks to me like you're overwriting your $sidebar array while looping over an array called $single? Commented May 10, 2012 at 18:47
  • @madbreaks you right, Besides $item is not filled any where... :( Commented May 10, 2012 at 18:49

4 Answers 4

1
array_search(key($sidebar), array_keys($sidebar));

Hmm .. not pretty. Use a for loop? :P

Sign up to request clarification or add additional context in comments.

1 Comment

The key is a string though, not a number.
0

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 = 0;

$sidebar = array(
    "Best of the Month" => $featuredBestMonth, 
    "Featured Content"  => $featuredContent
);

foreach($sidebar as $key => $item){
    if($item){   // will $item ever NOT evaluate to true?
        echo "<h3>".++$i.". $key</h3>";

        foreach($item as $single){
            echo "<p>$single[title]</p>";
        }
    }
}

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.

1 Comment

It's just the php for template files so it's a lot easier to see the html when it's all used in short tags. The $sidebar array is created in the controller.
0

I do not believe there is a way to do what you are asking with a string index (without of coursing using a separate counter variable). A for loop, or another loop with a counter is really the only way to do what you asked.

Comments

0

I recommend you use foreach for almost all your array loops needs:

foreach ($sidebar as $single) {}

And for count array elements just use count()

count ($sidebar);

if (is_array($sidebar))
foreach ($sidebar as $key => $single) :
?>
    <h3><?php echo $key; ?></h3>
    <p><?php echo $single['title']; ?></p>
<?
endforeach;

Final solution:

if (is_array($sidebar))
{
    $i = 0;
    foreach ($sidebar as $key => $item)
    {
        $i2 = ++ $i;
        echo "<h3>{$i2}.- $key</h3>\n";

        if (is_array($item))
        {
            $j = 0;
            foreach ($item as $single)
            {
                $j2 = ++ $j;
                echo "<p>{$j2}.- {$single['title']}</p>";
            };
        }
    }
}

7 Comments

Can I still get the key if I use foreach though?
foreach($sidebar as $key => $single){ ...
@silver89 Please test this (just edited) solution. I think it's the best approach.
Code updated at the top, $sidebar is the array of arrays so will always be an array.
I see. OK. But you still can replace the first while for a foreach. :) Looks better now... and don forget test with is_array() before walk for an array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.