123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
- declare (strict_types=1);
- namespace library\utils;
- // +----------------------------------------------------------------------
- // | [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2018-2020 rights reserved.
- // +----------------------------------------------------------------------
- // | Author: TABLE ME
- // +----------------------------------------------------------------------
- // | Date: 2020-09-03 21:20
- // +----------------------------------------------------------------------
- class Dtree {
- public $ret;
- public $arr; // 生成树型结构所需要的2维数组
- public $nbsp = " ";
- public $icon = array('│', '├', '└'); // 生成树型结构所需修饰符号,可以换成图片
- public $is_noselect = false;//是否不支持无下一级选项
- /**
- * 构造函数,初始化类
- * @param array 2维数组,例如:
- * array(
- * 1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
- * 2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'),
- * 3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'),
- * 4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'),
- * 5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
- * 6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
- * 7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
- * )
- */
- public function init($arr = array()) {
- $this->arr = $this->ret = NULL;
- $this->arr = $arr;
- return is_array($arr);
- }
- /**
- * 得到父级数组
- *
- * @param int
- * @return array
- */
- public function get_parent($myid) {
- $newarr = array();
- if(!isset($this->arr[$myid])) return FALSE;
- $pid = $this->arr[$myid]['pid'];
- $pid = $this->arr[$pid]['pid'];
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['pid'] == $pid) $newarr[$id] = $a;
- }
- }
- return $newarr;
- }
- /**
- * 得到子级数组
- *
- * @param int
- * @return array
- */
- public function get_child($myid) {
- $a = $newarr = array();
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['pid'] == $myid) $newarr[$id] = $a;
- }
- }
- return $newarr ? $newarr : FALSE;
- }
- /**
- * 获取子栏目json
- *
- * Enter description here ...
- * @param unknown_type $myid
- */
- public function creat_sub_json($myid, $str='') {
- $sub_cats = $this->get_child($myid);
- $n = 0;
- if(is_array($sub_cats)) foreach($sub_cats as $c) {
- $data[$n]['id'] = iconv(CHARSET,'utf-8',$c['catid']);
- if($this->get_child($c['catid'])) {
- $data[$n]['liclass'] = 'hasChildren';
- $data[$n]['children'] = array(array('text'=>' ','classes'=>'placeholder'));
- $data[$n]['classes'] = 'folder';
- $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']);
- } else {
- if($str) {
- @extract(array_iconv($c,CHARSET,'utf-8'));
- eval("\$data[$n]['text'] = \"$str\";");
- } else {
- $data[$n]['text'] = iconv(CHARSET,'utf-8',$c['catname']);
- }
- }
- $n++;
- }
- return json_encode($data);
- }
- private function have($list,$item){
- return(strpos(',,'.$list.',',','.$item.','));
- }
- }
|