dpitqpax07626 2016-02-02 09:05
浏览 42
已采纳

如何从平面阵列制作树 - php

I am trying to represent the whole array returned from Amazon S3 bucket in a tree structure one can browse.

The array example is following

$files[0] = 'container/798/';
$files[1] = 'container/798/logo.png';
$files[2] = 'container/798/test folder/';
$files[3] = 'container/798/test folder/another folder/';
$files[4] = 'container/798/test folder/another folder/again test/';
$files[5] = 'container/798/test folder/another folder/test me/';
$files[6] = 'container/798/test two/';
$files[7] = 'container/798/test two/logo2.png';

and this is what i am trying to achieve

http://i.stack.imgur.com/HBjvE.png   

so far i have only achieved differing the files and folder but not on different level with parent-child relation. The above mentioned array resides in $keys['files']. The code is following

$keys = json_decode($result,true);
$folders = array();
$files = array();
$i =0;
foreach ($keys['files'] as $key){
    if(endsWith($key, "/")){
        $exploded = explode('container/'.$_SESSION['id_user'].'/',$key);
        if(!empty($exploded[1]))
        $folders[$i]['name'] = substr($exploded[1],0,-1);
    }
    else{
        $exploded = explode('container/'.$_SESSION['id_user'].'/',$key);
        $files[$i]['name'] = $exploded[1];
        $files[$i]['size'] = "";
        $files[$i]['date'] = "";
        $files[$i]['preview_icon'] = "";
        $files[$i]['dimensions'] = "";
        $files[$i]['url'] = "";
    }
    $i++;
}

This is code just to show i am trying but its not complete or accurate. I don't know how to approach a logic that can give me the hierarchy i am showing the picture. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doujin8673 2016-02-02 16:47
    关注

    I don't know if this is the 'correct' way to do this, but if you want to make a recursive structure, then the easy way is to use a recursive function:

    $root = array('name'=>'/', 'children' => array(), 'href'=>'');
    
    function store_file($filename, &$parent){
    
        if(empty($filename)) return;
    
        $matches = array();
    
        if(preg_match('|^([^/]+)/(.*)$|', $filename, $matches)){
    
            $nextdir = $matches[1];
    
            if(!isset($parent['children'][$nextdir])){
                $parent['children'][$nextdir] = array('name' => $nextdir,
                    'children' => array(),
                    'href' => $parent['href'] . '/' . $nextdir);
            }
    
            store_file($matches[2], $parent['children'][$nextdir]);
        } else {
            $parent['children'][$filename] = array('name' => $filename,
                'size' => '...', 
                'href' => $parent['href'] . '/' . $filename);
        }
    }
    
    foreach($files as $file){
        store_file($file, $root);
    }
    

    Now, every element of root['children'] is an associative array that hash either information about a file or its own children array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题