站長資訊網
        最全最豐富的資訊網站

        分享一個生成文件層級樹類

        分享一個生成文件層級樹類

        根據 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 '<br/>';     echo '<br/>'; }

        推薦教程:《PHP教程》

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 欧洲精品视频在线观看| 日本精品久久久久中文字幕8| 国产亚洲精品AA片在线观看不加载 | 久久精品国产精品亜洲毛片| 黑人巨大精品欧美| 亚洲国产精品毛片av不卡在线| 久久精品一本到99热免费| 久久精品国产精品青草| 麻豆aⅴ精品无码一区二区| 亚洲日韩国产精品乱| 国产精品v欧美精品v日本精 | 无码囯产精品一区二区免费| 精品无码人妻一区二区三区不卡 | 精品亚洲麻豆1区2区3区| 亚洲精品网站在线观看不卡无广告| 国产成人A人亚洲精品无码| 在线观看91精品国产网站| 久久e热在这里只有国产中文精品99 | 99久久精品免费看国产免费| 91精品啪在线观看国产| 国产精品99久久久久久www| 国产精品视频网站你懂得| 精品无码人妻夜人多侵犯18| 在线精品亚洲| 午夜精品久久久久久影视777| 99精品国产在热久久无毒不卡| 国产精品永久久久久久久久久| 久久精品国产99久久无毒不卡| 国产观看精品一区二区三区| 久久国产精品久久精品国产| 久久成人影院精品777| www.亚洲精品.com| 91精品国产自产在线观看永久| 精品无人区无码乱码毛片国产 | 国产精品99久久久久久猫咪| 国产精品91av| 国产精品爱啪在线线免费观看| 久久性精品| 麻豆精品| 亚洲精品无码久久一线| 老司机亚洲精品影院|