栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

分享一个生成文件层级树类

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

分享一个生成文件层级树类

根据 php 递归读取文件夹生成文件树

class Tree
{
    public $arr = array();
    public $icon = array(
 '│',
 '├─',
 '└─'
    );
    public $ret;
    public function set_tree($arr = array())
    {
 $this->arr = $arr;
    }
    public function get_child($myid)
    {
 $newarr = array();
 if (is_array($this->arr)) {
     foreach ($this->arr as $id => $a) {
  if ($a['pid'] == $myid) {
      $newarr[$id] = $a;
  }
     }
 }
 return $newarr ? $newarr : false;
    }
    //获取带格式数组
    public function getArray($myid = 0, $sid = 0, $adds = '')
    {
 $number = 1;
 $child = $this->get_child($myid);
 if (is_array($child)) {
     $total = count($child);
     foreach ($child as $a) {
  $j = $k = '';
  if ($number == $total) {
      $j .= $this->icon[2];
  } else {
      $j .= $this->icon[1];
      $k = $adds ? $this->icon[0] : '';
  }
  $spacer = $adds ? $adds . $j : '';
  $a['name'] = $spacer . ' ' . $a['name'];
  $this->ret[] = $a;
  $fd = $adds . $k . '   ';
  $this->getArray($a['id'], $sid, $fd);
  $number++;
     }
 }
 return $this->ret;
    }
    //select
    public function get_tree($myid, $str, $sid = 0, $adds = '')
    {
 $number = 1;
 $child = $this->get_child($myid);
 if (is_array($child)) {
     $total = count($child);
     foreach ($child as $a) {
  $id = $a['id'];
  $j = $k = '';
  if ($number == $total) {
      $j .= $this->icon [2];
  } else {
      $j .= $this->icon [1];
      $k = $adds ? $this->icon [0] : '';
  }
  $spacer = $adds ? $adds . $j : '';
  $select = $id == $sid ? 'selected' : '';
  $this->ret .= sprintf($str, $id, $select, $spacer, $a['name']);
  $this->get_tree($id, $str, $sid, $adds . $k . ' ');
  $number++;
     }
 }
 return $this->ret;
    }
    //文件夹目录
    public function read_all_dir($dir, $onlyDir = true, $ignore = [])
    {
 $result = array();
 $handle = opendir($dir);
 if ($handle) {
     while (($file = readdir($handle)) !== false) {
  if (in_array($file, $ignore)) continue;
  if ($file != '.' && $file != '..') {
      $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
      if (is_dir($cur_path)) {
   $result[$file] = $this->read_all_dir($cur_path, $onlyDir);
      } else {
   if (!$onlyDir) {
$result[] = $file;
   }
      }
  }
     }
     closedir($handle);
 }
 return $result;
    }
    //数组转换
    public function arrshift($array, $pid = 0)
    {
 static $r = [];
 static $index = 1;
 if (is_array($array) && count($array) > 0) {
     foreach ($array as $k => $v) {
  $r[] = array(
      'id' => $index,
      'pid' => $pid,
      'name' => is_array($v) ? $k : $v
  );
  $index++;
  $this->arrshift($v, $index - 1);
     }
 }
 return $r;
    }
}

使用示例

$tree = new Tree ();
//文件夹遍历
$data = $tree->read_all_dir(realpath('../file_dir'), false, ['.git', '.idea', 'vendor']);
//转换成[['id','pid','name']]的二维数组
$data = $tree->arrshift($data);
$tree->set_tree($data); 
$data = $tree->getArray();
foreach ($data as $value) {
    echo $value['name'];
    echo '
'; echo '
'; }

推荐教程:《PHP教程》

以上就是分享一个生成文件层级树类的详细内容,更多请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/263090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号