加入收藏 | 设为首页 | 会员中心 | 我要投稿 十堰站长网 (https://www.0719zz.com/)- 混合云存储、网络、视频终端、云计算、媒体处理!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP实现无限级分类(递归方法)

发布时间:2023-02-16 13:58:52 所属栏目:PHP 来源:互联网
导读:实现代码: ?PHP include(tree.class.PHP); //具有这个结构的数组,不是这样的话,可以改类的实现 $list = array( 1 = array(id = 1,pid = 0,name = 一级栏目一), 2 = array(id = 2,name = 一级栏目二), 3 = array(id = 3,pid = 1,name = 二级栏目一), 4 = a

 
  $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,$c['catname']);
 
  } else {
 
  if($str) {
 
  @extract(array_iconv($c,CHARSET,'utf-8'));
 
  eval("$data[$n]['text'] = "$str";");
 
  } else {
 
  $data[$n]['text'] = iconv(CHARSET,$c['catname']);
 
  }
 
  }
 
  $n++;
 
  }
 
  return json_encode($data);
 
  }
 
  private function have($list,$item){
 
  return(strpos(','.$list.',','.$item.','));
 
  }
 
  }
 
  /**
 
   * +------------------------------------------------
 
   * 格式化数组
 
   * +------------------------------------------------
 
   */
 
  function getArray($myid = 0,$adds = '') {
 
      $number = 1;
 
      $child = $this->get_child($myid);
 
      if (is_array($child)) {
 
          $total = count($child);
 
          foreach ($child as $id => $a) {
 
              $j = $k = '';
 
              if ($number == $total) {
 
                  $j .= $this->icon[2];
 
              } else {
 
                  $j .= $this->icon[1];
 
                  $k = $adds ? $this->icon[0] : '';
 
              }
 
              $spacer = $adds ? $adds . $j : '';
 
              @extract($a);
 
              $a[$name] = $spacer . $a[$name];
 
              $this->ret[$a[$id]] = $a;
 
              $fd = $adds . $k . $this->nbsp;
 
              $this->getArray($id,$fd);
 
              $number++;
 
          }
 
      }
 
      return $this->ret;
 
  }
 
  ?>

(编辑:十堰站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读