zoukankan      html  css  js  c++  java
  • 查询无限极分类

     1 /**
     2  * User: finn.lee
     3  * Date: 2016/3/5 14:25
     4  */
     5 $rows = array(
     6     0=>array(
     7         'cat_id' => 1,
     8         'name' => 'dev',
     9         'pid' => 0
    10     ),
    11     1=>array(
    12         'cat_id' => 2,
    13         'name' => 'php',
    14         'pid' => 1
    15     ),
    16     2=>array(
    17         'cat_id' => 3,
    18         'name' => 'smarty',
    19         'pid' => 2
    20     ),
    21     3=>array(
    22         'cat_id' => 4,
    23         'name' => 'life',
    24         'pid' => 0
    25     ),
    26     4=>array(
    27         'cat_id' => 5,
    28         'name' => 'pdo',
    29         'pid' => 2
    30     ),
    31     5=>array(
    32         'cat_id' => 6,
    33         'name' => 'pdo-mysql',
    34         'pid' => 5
    35     ),
    36     6=>array(
    37         'cat_id' => 7,
    38         'name' => 'java',
    39         'pid' => 1
    40     )
    41 );
    42 
    43 // 72648
    44 // 84072
    45 
    46 function findChild(&$arr,$id){
    47 
    48     $childs=array();
    49     foreach ($arr as $k => $v){
    50         if($v['pid']== $id){
    51             $childs[]=$v;
    52         }
    53     }
    54     return $childs;
    55 }
    56 function build_tree($root_id){
    57     global $rows;
    58     $childs=findChild($rows,$root_id);
    59     if(empty($childs)){
    60         return null;
    61     }
    62     foreach ($childs as $k => $v){
    63         $rescurTree=build_tree($v['cat_id']);
    64         if( null !=   $rescurTree){
    65             $childs[$k]['childs']=$rescurTree;
    66         }
    67     }
    68     return $childs;
    69 }
    70 
    71 
    72 $tree=build_tree(0);
    73 
    74 echo memory_get_usage();
    75 
    76 print_r($tree);

    注: $rows是活的 , $root_id是活的

  • 相关阅读:
    cocos2d-x Mask的实现及优化
    cocos2d-x CCTableView
    cocos2d-x CCScrollView
    网络服务端开发
    跨平台web调试代理工具---whistle
    探索c#之一致性Hash详解
    Redis性能问题排查解决手册
    Redis的Aof被阻塞原因调查
    AOF 持久化策略
    如何招到一个靠谱的程序员
  • 原文地址:https://www.cnblogs.com/finnlee/p/5244932.html
Copyright © 2011-2022 走看看