php 非递归无限分类 性能比递归性能高

<?php  
    function genTree($items,$id='id',$pid='pid',$son = 'children'){  
        $tree = array();  
        $tmpMap = array();  
          
        foreach ($items as $item) {  
            $tmpMap[$item[$id]] = $item;  
        }  
          
        foreach ($items as $key => $item) {  
            if (isset($tmpMap[$item[$pid]])) {  
                $tmpMap[$item[$pid]][$son][$key] = &$tmpMap[$key];  
            } else {  
                $tree[] = &$tmpMap[$item[$id]];  
            }  
        }  
        unset($tmpMap);  
        return $tree;  
    }  
  
    $items1 = array(  
            array('id' => 1,  'pid' => 0,  'name' => '一级' ),  
            array('id' => 11, 'pid' => 0,  'name' => '一级' ),  
            array('id' => 2,  'pid' => 1,  'name' => '二级' ),  
            array('id' => 10, 'pid' => 11, 'name' => '二级' ),  
            array('id' => 3,  'pid' => 1,  'name' => '二级' ),  
            array('id' => 12, 'pid' => 11, 'name' => '二级' ),  
            array('id' => 9,  'pid' => 1,  'name' => '二级' ),  
            array('id' => 14, 'pid' => 1,  'name' => '二级' ),  
            array('id' => 4,  'pid' => 9,  'name' => '三级' ),  
            array('id' => 6,  'pid' => 9,  'name' => '三级' ),  
            array('id' => 7,  'pid' => 4,  'name' => '四级' ),  
            array('id' => 8,  'pid' => 4,  'name' => '四级' ),  
            array('id' => 5,  'pid' => 4,  'name' => '四级' ),  
            array('id' => 13, 'pid' => 4,  'name' => '四级' ),  
            array('id' => 15, 'pid' => 8,  'name' => '五级' ),  
            array('id' => 16, 'pid' => 8,  'name' => '五级' ),  
            array('id' => 17, 'pid' => 8,  'name' => '五级' ),  
            array('id' => 18, 'pid' => 16, 'name' => '六级' ),  
    );   
    echo "<pre>";  
    echo "父节点:<br/>";  
    print_r(genTree($items1));  

  

<?php  
    header("Content-type:text/html;charSet=utf8");  
    /** 
     * 创建父节点树形数组 
     * 参数 
     * $ar 数组,邻接列表方式组织的数据 
     * $id 数组中作为主键的下标或关联键名 
     * $pid 数组中作为父键的下标或关联键名 
     * 返回 多维数组 
     **/  
    function find_parent($ar, $id='id', $pid='pid') {   
        foreach($ar as $v) $t[$v[$id]] = $v;  
        foreach ($t as $k => $item){  
            if( $item[$pid] ){  
            if( ! isset($t[$item[$pid]]['parent'][$item[$pid]]) )  
                $t[$item[$id]]['parent'][$item[$pid]] = &$t[$item[$pid]];  
            }  
        }   
        return $t;  
    }  
  
    /** 
     * 创建子节点树形数组 
     * 参数 
     * $ar 数组,邻接列表方式组织的数据 
     * $id 数组中作为主键的下标或关联键名 
     * $pid 数组中作为父键的下标或关联键名 
     * 返回 多维数组 
     **/  
    function find_child($ar, $id='id', $pid='pid') {  
        foreach($ar as $v) $t[$v[$id]] = $v;  
        foreach ($t as $k => $item){  
            if( $item[$pid] ) {  
                $t[$item[$pid]]['child'][$item[$id]] = &$t[$k];  
            }  
        }  
        return $t;  
    }  
  
      
    $data = array(  
      array('ID'=>1, 'PARENT'=>0, 'NAME'=>'祖父'),  
      array('ID'=>2, 'PARENT'=>1, 'NAME'=>'父亲'),  
      array('ID'=>3, 'PARENT'=>1, 'NAME'=>'叔伯'),  
      array('ID'=>4, 'PARENT'=>2, 'NAME'=>'自己'),  
      array('ID'=>5, 'PARENT'=>4, 'NAME'=>'儿子'),  
    );  
  
    $p = find_parent($data, 'ID', 'PARENT');  
    $c = find_child($data, 'ID', 'PARENT');  
    echo "<pre>";  
    echo "父节点:<br/>";  
    print_r($p);  
    echo "子节点:<br/>";  
    print_r($c);  

  

posted @ 2017-05-19 11:29  /折磨  阅读(160)  评论(0编辑  收藏  举报