开发者

PHP loop only going around 9 times instead of ~45?

This is my code:

for($i=0; $i < count($badLinkHolder); $i++)
    {
        if($badLinkHolder[$i][0] != "")
        {
            echo "<tr><td>{$i}&开发者_Python百科lt;/td><td>{$badLinkHolder[$i][0]}</td><td>{$badLinkHolder[$i][1]}</td></tr>";
        }
    }

Now, $badLinkHolder has around 45 entries which I can confirm by using print_r(). For some reason, this loop is only going around 9 times and I can't retrieve all of my data from the array.

Any help on this? I'm completely baffled.


Firstly, how did you know that it is going only 9 times? My guess is that you've seen only 9 rows in your table thus assuming that your loop only goes 9 times. You have a if statement that states that if ($badLinkHolder[$i][0] != ""), only then you will print out the row.

On top of this, foreach would be better than for looping an array in PHP.

$i = 0;
foreach($badLinkHolder as $holder)
{
    if($holder[0] != "")
    {
        echo "<tr><td>{$i}</td><td>{$holder[0]}</td><td>{$holder[1]}</td></tr>";
    }
    $i++;
}


Your loop looks fine. However I'm not sure of the content of the array and I would suggest that you first try removing the if statement and replacing it with some dummy text. For example:

for($i = 0; $i < count($badLinkHolder); $i++)
{
  echo "test $i";
}

That way, you can make sure that you are passing through the loop the correct number of times and it is not an error with the code inside the loop. Also, have you echoed the returned value of count($badLinkHolder)?


Oh gawd, I'm an idiot. I was looking at the entries for $badLinkHolder before it went through a few functions that weren't working as normal and filled it with loads of null values.

Problem solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜