php 三级分类递归

 * 分类递归
 * $categorys 数值  $pId父类id $l 等级
 */
function getParents($categorys,$pId = 0,$l=0){
    $list =array();

    foreach ($categorys as $k=>$v){

        if ($v['parentid'] == $pId){
            unset($categorys[$k]);
            if ($l < 2){
            //小于三级
                $v['children'] = $this->getParents($categorys,$v['id'],$l+1);
            }

            $list[] = $v;

        }
    }
    return $list;

}

$category = array(
‘0’ => array( ‘id’ => 1, ‘parentid’ => 0 ),
‘1’ => array( ‘id’ =>2, ‘parentid’ => 1 ),
‘2’ => array( ‘id’ =>3, ‘parentid’ => 2),
);

c a t e = g e t P a r e n t s ( cate =getParents( cate=getParents(category);
var_export($cate);

你可能感兴趣的:(php)