zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    JVM系列三:JVM参数设置、分析
    JVM系列二:GC策略&内存申请、对象衰老
    HotSpot VM GC 的种类
    2.静态库和动态库
    1.GCC编译过程
    6.树
    5.队列
    4.栈
    3.线性表
    2.算法
  • 原文地址:https://www.cnblogs.com/e0yu/p/7360857.html
Copyright © 2011-2022 走看看