OneThink生成分类树方法(list_to_tree)使用!

具体方法:

Application / Common / Common / function.php 下的 224行:

function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId =  $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
}

具体使用:例如这样的一个数组:

array(9) {
  [0]=>
  array(6) {
    ["id"]=>
    string(2) "39"
    ["title"]=>
    string(12) "医院概况"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    string(1) "4"
    ["model"]=>
    string(1) "2"
  }
  [1]=>
  array(6) {
    ["id"]=>
    string(2) "42"
    ["title"]=>
    string(12) "新闻中心"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "2"
  }
  [2]=>
  array(6) {
    ["id"]=>
    string(2) "46"
    ["title"]=>
    string(12) "问答分类"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "5"
  }
  [3]=>
  array(6) {
    ["id"]=>
    string(2) "40"
    ["title"]=>
    string(12) "医院简介"
    ["pid"]=>
    string(2) "39"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    string(1) "4"
    ["model"]=>
    string(1) "2"
  }
  [4]=>
  array(6) {
    ["id"]=>
    string(2) "41"
    ["title"]=>
    string(12) "领导班子"
    ["pid"]=>
    string(2) "39"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "2"
  }
  [5]=>
  array(6) {
    ["id"]=>
    string(2) "43"
    ["title"]=>
    string(12) "医院新闻"
    ["pid"]=>
    string(2) "42"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "2"
  }
  [6]=>
  array(6) {
    ["id"]=>
    string(2) "44"
    ["title"]=>
    string(12) "行业新闻"
    ["pid"]=>
    string(2) "42"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    string(1) "4"
    ["model"]=>
    string(1) "2"
  }
  [7]=>
  array(6) {
    ["id"]=>
    string(2) "45"
    ["title"]=>
    string(12) "文化信息"
    ["pid"]=>
    string(2) "42"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "2"
  }
  [8]=>
  array(6) {
    ["id"]=>
    string(2) "47"
    ["title"]=>
    string(12) "其他新闻"
    ["pid"]=>
    string(2) "43"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    string(1) "4"
    ["model"]=>
    string(1) "2"
  }
}

进行分类:

$cate =   list_to_tree($cate);    //生成分类树
pd($cate);

效果:

array(3) {
  [0]=>
  array(7) {
    ["id"]=>
    string(2) "39"
    ["title"]=>
    string(12) "医院概况"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    string(1) "4"
    ["model"]=>
    string(1) "2"
    ["_child"]=>
    array(2) {
      [0]=>
      array(6) {
        ["id"]=>
        string(2) "40"
        ["title"]=>
        string(12) "医院简介"
        ["pid"]=>
        string(2) "39"
        ["allow_publish"]=>
        string(1) "1"
        ["cattype"]=>
        string(1) "4"
        ["model"]=>
        string(1) "2"
      }
      [1]=>
      array(6) {
        ["id"]=>
        string(2) "41"
        ["title"]=>
        string(12) "领导班子"
        ["pid"]=>
        string(2) "39"
        ["allow_publish"]=>
        string(1) "1"
        ["cattype"]=>
        NULL
        ["model"]=>
        string(1) "2"
      }
    }
  }
  [1]=>
  array(7) {
    ["id"]=>
    string(2) "42"
    ["title"]=>
    string(12) "新闻中心"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "2"
    ["_child"]=>
    array(3) {
      [0]=>
      array(7) {
        ["id"]=>
        string(2) "43"
        ["title"]=>
        string(12) "医院新闻"
        ["pid"]=>
        string(2) "42"
        ["allow_publish"]=>
        string(1) "1"
        ["cattype"]=>
        NULL
        ["model"]=>
        string(1) "2"
        ["_child"]=>
        array(1) {
          [0]=>
          array(6) {
            ["id"]=>
            string(2) "47"
            ["title"]=>
            string(12) "其他新闻"
            ["pid"]=>
            string(2) "43"
            ["allow_publish"]=>
            string(1) "1"
            ["cattype"]=>
            string(1) "4"
            ["model"]=>
            string(1) "2"
          }
        }
      }
      [1]=>
      array(6) {
        ["id"]=>
        string(2) "44"
        ["title"]=>
        string(12) "行业新闻"
        ["pid"]=>
        string(2) "42"
        ["allow_publish"]=>
        string(1) "1"
        ["cattype"]=>
        string(1) "4"
        ["model"]=>
        string(1) "2"
      }
      [2]=>
      array(6) {
        ["id"]=>
        string(2) "45"
        ["title"]=>
        string(12) "文化信息"
        ["pid"]=>
        string(2) "42"
        ["allow_publish"]=>
        string(1) "1"
        ["cattype"]=>
        NULL
        ["model"]=>
        string(1) "2"
      }
    }
  }
  [2]=>
  array(6) {
    ["id"]=>
    string(2) "46"
    ["title"]=>
    string(12) "问答分类"
    ["pid"]=>
    string(1) "0"
    ["allow_publish"]=>
    string(1) "1"
    ["cattype"]=>
    NULL
    ["model"]=>
    string(1) "5"
  }
}

与其相反的还有 tree_to_list : Application / Common / Common / function.php 下的 258行:

function tree_to_list($tree, $child = '_child', $order='id', &$list = array()){
    if(is_array($tree)) {
        foreach ($tree as $key => $value) {
            $reffer = $value;
            if(isset($reffer[$child])){
                unset($reffer[$child]);
                tree_to_list($value[$child], $child, $order, $list);
            }
            $list[] = $reffer;
        }
        $list = list_sort_by($list, $order, $sortby='asc');
    }
    return $list;
}

 

posted @ 2017-08-14 23:18  帅到要去报警  阅读(829)  评论(0编辑  收藏  举报