递归处理无限级分类为树形数组

处理一个无限分类问题,之前是用的多级ajax实现,但是衡量了一下,总数据量不大,决定用树形数组组织一下数据形成js文件,直接放到页面里



 $strV){
            if($intK > 0 && isset($arrmdata[$intK])){
               $arrData[$intL][$intK] = array($strV);
               formate_arr($arrData[$intL],$arrmdata,$intK);
            }
        }
    }
    if(count($arrmdata) == 0)
        return $arrData;
}
$arrService = $arrMdata = $arrData = array();
$arrTmp = array(
            '0'=>array('id'=>1,'parentid'=>0,'name'=>'111'),
            '1'=>array('id'=>2,'parentid'=>0,'name'=>'222'),
            '2'=>array('id'=>3,'parentid'=>1,'name'=>'333'),
            '3'=>array('id'=>4,'parentid'=>1,'name'=>'444'),
            '4'=>array('id'=>5,'parentid'=>3,'name'=>'555'),
            '5'=>array('id'=>6,'parentid'=>3,'name'=>'666'),
            '6'=>array('id'=>7,'parentid'=>4,'name'=>'777'),
            '7'=>array('id'=>8,'parentid'=>7,'name'=>'888'),
            '8'=>array('id'=>9,'parentid'=>2,'name'=>'999'),
            '9'=>array('id'=>10,'parentid'=>9,'name'=>'000'),
            '10'=>array('id'=>11,'parentid'=>9,'name'=>'101')
        );
foreach ($arrTmp as $arrSingle) {
        if(!is_array($arrMdata[$arrSingle['parentid']]))
            $arrMdata[$arrSingle['parentid']] = array();
        $arrMdata[$arrSingle['parentid']][$arrSingle['id']] = $arrSingle['name'];

}
formate_arr($arrData,$arrMdata,0);

$arrData = $arrData[0];

print_r($arrData);
?>


你可能感兴趣的:(php)