Invalid argument supplied for foreach! [duplicate]

戏子无情 提交于 2020-01-25 07:15:27

问题


Possible Duplicate:
Invalid argument supplied for foreach()

I have the following code:

<?
foreach($format as $form)
{
    echo $form;
    ?>
    <ul>
        <?
        $s = $database->onlineFormatUsers($form);
        while($row=mysql_fetch_assoc($s))
        {
            $username=$row['username'];
            $id=$row['id'];?>
            <li><a href="../userprofile.php?id=<?echo $id?>"><?echo "$username";?></a></li>
        <?
        }
        ?>
    </ul>
    <?
}
?>

<? 
//the active formats
$f = $database->activeFormats();
while($row=mysql_fetch_assoc($f))
{
    $format=$row['name'];
}
?>

It is saying its an invalid argument? Any reason why? Thanks


回答1:


$format is probably not an array.

Wrap the foreach block in an if(is_array($format)) { } block or cast it to an array by doing $format = (array)$format.




回答2:


are you sure $format is an array ? put an

<?php echo gettype($format); ?>

before the foreach loop




回答3:


$format is not array or not exists! Before foreach

if(is_array($format)){  
   foreach($format ...
}


来源:https://stackoverflow.com/questions/3180915/invalid-argument-supplied-for-foreach

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!