&引用;试图获取非对象的属性“;PHP7错误(在PHP5.4中工作)

&引用;试图获取非对象的属性“;PHP7错误(在PHP5.4中工作),php,Php,这是我的职责: function get_cat_option($data, $pid, $cid=0, $parent=0){ static $i = 1; $tab = str_repeat(" ",$i); static $a = 0; $pusher = "-"; $showPusher = str_repeat($pusher,$a); if(isset($data[$parent]) && $data[$parent]

这是我的职责:

function get_cat_option($data, $pid, $cid=0, $parent=0){

    static $i = 1;
    $tab = str_repeat(" ",$i);
    static $a = 0;
    $pusher = "-";
    $showPusher = str_repeat($pusher,$a);
    if(isset($data[$parent]) && $data[$parent])
    {
        $html = "$tab";
        $i++;
        foreach($data[$parent] as $v)
        {

            $a++;           
            $child = get_cat_option($data, $pid, $cid, $v->category_id);
            if($v->category_parent_id == 0)
            {
                $listChild = "";
            }
            if($v->category_id == $pid)
            {
                $selected = ' selected';
            }
            else
            {
                $selected = '';
            }
            if($v->category_id == $cid)
            {
                $disabled = ' disabled';
            }
            else
            {
                $disabled = '';
            }
            $html .= "$tab";
            $html .= '<option value="'.$v->category_id.'"'.$selected.$disabled.'>'.$showPusher.' '.$v->category_title.'</option>';
            $a--;
            if($child)
            {
                $i--;
                $html .= $child;
                $html .= "$tab";
            }
        }
        $html .= "$tab";
        return $html;
    }
    else
    {
        return false;
    }
}
错误行是:

$child = get_cat_option($data, $pid, $cid, $v->category_id);   
这段代码在PHP5.4中运行得非常好,但现在我使用的是PHP7


如何修复它?

错误很可能是指
$v->category\u id
参数。如果
category\u id
$v
中不存在,则抛出错误

在循环使用
$data[$parent]
对象之前,请验证该对象是否正确:

var_dump($data[$parent]);

很可能$v中没有属性“category_id”,因此它抛出了一个警告。您可以使用

if (isset($v->category_id)) {
// do something
}
还是美国??运算符设置默认值

$category_id = $v->category_id ?? $fallback;
请不要添加垃圾文本来绕过堆栈溢出的内容过滤器。他们在那里是有原因的。修正了。。。在foreach中($data[$parent]作为$v)将$data[$parent]更改为$data。我不知道该说什么……:)
if (isset($v->category_id)) {
// do something
}
$category_id = $v->category_id ?? $fallback;