zoukankan      html  css  js  c++  java
  • php无限分类 构建树形结构

    <?php
    
    class Classification {
    	const PARENT_ID = 'parentid';
    	const ID = 'id';
    	const CHILDREN = 'children';
    
    	public static function getTree($items) {
    		$children = [];
    		// group by parent id
    		foreach ($items as &$item) {
    			$children[ $item[self::PARENT_ID] ][] = &$item;
    			unset($item);
    		}
    		foreach ($items as &$item) {
    			$pid = $item[self::ID];
    			if (array_key_exists($pid, $children)) {
    				$item[self::CHILDREN] = $children[ $pid ];
    			}
    			unset($item);
    		}
    		return $children[0];
    	}
    }
    

      test:

    <?php
    
    $items = [
    	['id' => 1,  'parentid' => 0, 'name' => 'PHP'],
    	['id' => 2, 'parentid' => 1, 'name' => 'PHP_Framework'],
    	['id' => 42, 'parentid' => 1, 'name' => 'DevTools'],
    	['id' => 3, 'parentid' => 2, 'name' => 'ThinkPHP5'],
    	['id' => 4, 'parentid' => 2, 'name' => 'Laravel'],
    	['id' => 43, 'parentid' => 42, 'name' => 'PHPStorm'],
    	['id' => 44, 'parentid' => 42, 'name' => 'EclipsePDT'],
    ];
    shuffle($items);
    
    echo '<pre>';
    $a = array_map(function($item) {
    	return $item['name'];
    }, $items);
    print_r($a);
    
    $t = Classification::getTree($items);
    var_dump($t);
    // echo json_encode($t);
    

      

    Thinkphp Model

    <?php
    namespace appmodel;
    use thinkModel;
    class Link extends Model {
        protected $pk = 'id';
        protected $field = ['des', 'source', 'target', 'structid'];
        /**
         * @param $items
         * $items = array(
         *            array('id' => 42, 'parentid' => 1),
         *            array('id' => 43, 'parentid' => 42),
         *            array('id' => 1,  'parentid' => 0));
         * @return mixed
         * Array (
         *   [0] => Array(
         *     [id] => 1
         *     [parentid] => 0
         *     [childs]   => Array(
         *       [0] => Array (
         *          [id] => 42
         *          [parentid] => 1
         *          [childs] => Array(
         *             [0] => Array(
         *                [id] => 43
         *                [parentid] => 42
         *             )
         *          )
         *       )
         *     )
         *   )
         * )
         */
        public static function buildTree($items) {
            $childs = array();
            foreach($items as &$item) {
                $childs[$item['parentid']][] = &$item;
                unset($item);
            }
            foreach($items as &$item) {
                if (isset($childs[$item['id']])) {
                    $item['childs'] = $childs[$item['id']];
                }
                unset($item);
            }
            return $childs[0];
        }
        /**
         * 节点id下一级节点id数组
         */
        public function listChildNodeId_r($nodeid) {
            $a = $this->listChildNodeId($nodeid);
            $list = [];  // recursively
            foreach ($a as $it) {
                array_push($list, $it);
                $ca = $this->listChildNodeId($it);
                foreach ($ca as $ci) {
                    array_push($list, $ci);
                }
            }
            return $list;
        }
        // 节点id下一级节点id数组
        private function listChildNodeId($parentid) {
            $where = ['source' => $parentid];
            $a = $this->field('target')->where($where)->select();
            $t = [];
            foreach ($a as $item) {
                array_push($t, $item['target']);
            }
            unset($a);
            return $t;
        }
    }
    

      

  • 相关阅读:
    VCSA 6.5 升级 VCSA 6.7
    使用再生龙Clonezilla备份还原Linux系统
    gulp前端自动化构建工具学习笔记(mac)
    Echarts基本图表的学习笔记
    jQuery中$.ajax()用法
    jQuery实现淡入淡出轮播图带左右按钮及下方小圆点
    js解析XMl文件,兼容IE、Firefox、谷歌
    HTML<marquee>标签实现滚动公告通知、广告的效果
    画太极
    让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
  • 原文地址:https://www.cnblogs.com/mingzhanghui/p/9168357.html
Copyright © 2011-2022 走看看