[BUG反饋]function get_parent_category() 死迴圈

發表於2020-04-04
Admin/Common/function.php

function get_parent_category($cid){
    if(empty($cid)){
        return false;
    }
    $cates  =   M('Category')->where(array('status'=>1))->field('id,title,pid')->order('sort')->select();
    $child  =   get_category($cid);    //獲取引數分類的資訊
    $pid    =   $child['pid'];
    $temp   =   array();
    $res[]  =   $child;
    
    while(true){
        foreach ($cates as $key=>$cate){
            if($cate['id'] == $pid){
                $pid = $cate['pid'];
                array_unshift($res, $cate);    //將父分類插入到陣列第一個元素前
            }
        }
        if($pid == 0){
            break;
        }
    }
    return $res;
}
此程式碼有死迴圈,請檢查解決: 測試資料:
Array
(
    [0] => Array
        (
            [id] => 1
            [title] => 部落格
            [pid] => 0
        )

    [1] => Array
        (
            [id] => 39
            [title] => test
            [pid] => 1
        )


)
回覆

相關文章