当前位置:编程学习 > php >>

求教:PHP如何将一个无限分类转换为一个易做图数组?

如何输出类似如下易做图数组:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 新闻
            [parent] => Array
                (
                  [0] => Array
                   (
                      [id] => 2
                      [name] => PHP新闻
                      [parent] => Array
                        (
                          [0] => Array
                           (
                            [id] => 3
                            [name] => PHP6.0出来了
                           )
                        )
                    )
                   [1] => Array
                    (
                      [id] => 6
                      [name] => mysql新闻
                      [parent] => Array
                        (
                          [0] => Array
                          (
                            [id] => 7
                            [name] => 暂无mysql新闻
                          )
                        )
                   )
                )
        )
    [1] => Array
        (           
        )
 
)

答案:while($rs = mysql_fetch_array($result)){
 $rows[] = array('id'=>$rs['Id'],'name'=>$rs['Title'],'parentid'=>$rs['Pid']);

   
 function findChild(&$arr,$id){       
     $childs=array(); 
      foreach ($arr as $k => $v){ 
          if($v['parentid']== $id){ 
               $childs[]=$v;              
          }             
     }         
     return $childs;                
 } 
 function build_tree($root_id){ 
     global $rows; 
     $childs=findChild($rows,$root_id); 
     if(empty($childs)){ 
         return null; 
     } 
    foreach ($childs as $k => $v){ 
        $rescurTree=build_tree($v['id']); 
        if( null !=   $rescurTree){  
        $childs[$k]['childs']=$rescurTree; 
        } 
    } 
     return $childs; 
 }   
 $tree=build_tree(0);  
 print_r($tree);     
 ?>
SDDDDDDDDDDDDDDDDDDDDDDDD

上一个:PHP显示很多数据库信息,如何自动分页呢?代码
下一个:帮写段很简单的代码。。。。ASP或PHP的

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,