Foreach PHP错误

时间:2012-04-15 17:23:38

标签: php foreach

我在PHP文件中收到以下foreach错误,我不知道如何修复它。有没有人有任何想法?

当我加载页面时,我得到了这个:

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/merge/class/global_functions.php on line 61

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/merge/class/global_functions.php on line 89

我的/class/global_functions.php的第61行和第89行如下:

Here is my code from line 61 to line 98:

    foreach($GLOBALS['userpermbit'] as $v)
    {
        if(strstr($v['perm'],'|'.$pageperm_id[0]['id'].'|'))
            return true;
    }

    //if they dont have perms and we're not externally including functions return false
    if ($GLOBALS['external'] != true) return false; return true;

}

//FUNCTION: quick perm check using perm info from the onload perm check 
function stealthPermCheck($req)
{
    #if theyre an admin give them perms
    if(@in_array($GLOBALS['user'][0]['id'], $GLOBALS['superAdmins']))
            return true;    

    if(!is_numeric($req))
    {
        #if the req is numeric we need to match a title, not a permid. So try to do that
        foreach($GLOBALS['userpermbit'] as $v)
        {
            if(stristr($v['title'],$req))
                return true;
        }
    }else{
        #check if they have perms numerically if so return true
        foreach($GLOBALS['userpermbit'] as $v)
        {
            if(strstr($v['perm'],'|'.$req.'|'))
                return true;
        }
    }

    #if none of this returned true they dont have perms, return false
    return false;
}

4 个答案:

答案 0 :(得分:4)

foreach仅在变量为arrayobject时有效。

如果您提供其他内容,则会看到您看到的错误:

Warning: Invalid argument supplied for foreach() in ...

要停止该错误,请确保您传递给foreach的变量是arrayobject

邪恶的php coderz如果他们希望它通常是一个数组但是太懒了,不能检查任何事情,因为生命太短了,这就是应对它:

foreach ((array) @$prunzels as $do_not_care)
{
}

我强烈推荐它,因为你无论如何都使用$GLOBALS,这让我相信你想要在PHP邪恶中升级。

答案 1 :(得分:2)

将您的代码更改为69行:&在89上做同样的事情

$ GLOBALS ['userpermbit']:这可能是空白&不被foreach称为数组。

$u_per_arr = $GLOBALS['userpermbit'];
if(!is_array($u_per_arr)) {
 $u_per_arr = array();
}

foreach($u_per_arr as $v)

答案 2 :(得分:1)

$GLOBALS['userpermbit']未设置或不是数组。你需要检查它的初始化位置,或者它是否出错。试着给我们更多的背景。

答案 3 :(得分:0)

错误说,错误类型的变量已传递给foreach()构造。第89行出现错误。

foreach()构造期望第一个参数是一个数组。您在{89}中用作$userpermbit构造参数的foreach()变量似乎不是数组类型。

在代码中搜索$userpermbit的任何出现位置,并找出它的设置位置。更正它以将$userpermbit设置为数组。