qiuxis 2016-09-30 01:10:50 3702次浏览 0条评论 1 0 0

// classid , bclassid , islast
// 无限级分类

$cats = [
    ['classid' => 1, 'bclassid' => 0, 'classname' => 'classid_1', 'islast' => 0],
    ['classid' => 2, 'bclassid' => 0, 'classname' => 'classid_2', 'islast' => 0],
    ['classid' => 3, 'bclassid' => 1, 'classname' => 'classid_3', 'islast' => 0],
    ['classid' => 4, 'bclassid' => 6, 'classname' => 'classid_4', 'islast' => 1],
    ['classid' => 5, 'bclassid' => 1, 'classname' => 'classid_5', 'islast' => 1],
    ['classid' => 6, 'bclassid' => 2, 'classname' => 'classid_6', 'islast' => 0],
];

/*
需求:最终呈现

---|classid_1
---|---|classid_3
---|---|---|classid_4
---|---|classid5
---|classid_2
---|---|classid_6


*/


function getCatTree($cats , $bclassid = 0, $nu = 0 )
{
    $bx = '---|' ;
    $nu++ ;
    foreach ($cats as $cat){
        $catid		= $cat['classid'];
        $catname	= $cat['classname'];
        $catbid		= $cat['bclassid'];
        $islast     = $cat['islast'];
        if ($catbid == $bclassid) {
            echo str_repeat($bx, $nu) . $catname . ($islast ? '_last' : '') . PHP_EOL  ;
            getCatTree($cats, $catid, $nu ) ;
        }
    }
}

 getCatTree($cats );
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册