资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php生成文件层级树类-创新互联

今天小编给大家分享的是php生成文件层级树类的方法。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。                                                           

目前创新互联建站已为上千多家的企业提供了网站建设、域名、虚拟空间、网站运营、企业网站设计、黄南州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

根据 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生成文件层级树类的方法,具体使用情况还需要大家自己动手实验使用过才能领会。如果想了解更多相关内容,欢迎关注创新互联成都网站设计公司行业资讯频道!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:php生成文件层级树类-创新互联
文章网址:http://cdkjz.cn/article/cssdph.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220