目录结构的深层递归数组在PHP [英] Deep recursive array of directory structure in PHP

查看:131
本文介绍了目录结构的深层递归数组在PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的硬盘驱动器上的一些文件夹到一个数组中。

I'm trying to put some folders on my hard-drive into an array.

例如,度假的照片。
比方说,我们有这样的结构:

For instance, vacation pictures. Let's say we have this structure:


  • 设置1

    • 设置(一)项1

    • 设置1的项目2

    • 项...设置1


    • 设置2的子集1

      • 设置2的子集(一)项1

      • 项...集2的子集1

      我想有这样的事情,作为一个数组。

      这意味着我有1大的数组,数组中的多个阵列。每一组子集和获得它自己的数组。

      I want to have something like that, as an array.
      Meaning I have 1 big array and in that array are more arrays. Each set and subset gets its own array.

      我想使它看起来像这样:

      I'm trying to make it look something like this:

      Array
      (
          [Set 1] => Array([0] => Item 1 of Set 1, [1] => Item 1 of Set 1,...)
          [Set 2] => Array([Subnet 1] => Array([0] => Item 1 of Subset 1 of Set 2,[1] => ...), [Subnet 2] => Array([0] => ..., ..., ...), ..., [0] => Random File)
          [set 3] => Array(...)
          ...
      )
      

      我碰到这个就来了: http://www.the-art-of-web.com / PHP / dirlist /

      不过,这不是我要找的。我一直在用爱管闲事,但它给我添麻烦。

      But that's not what I'm looking for. I've been meddling with it but it's giving me nothing but trouble.

      下面是对于较大的分辨率为例,查看源(点击无明显...)。

      Here's an example, view source for larger resolution(no clicking apparently...).

      推荐答案

      我建议使用 DirectoryIterator 以建立您的阵列

      I recommend using DirectoryIterator to build your array

      下面是一个片段我扔在一起真正的快,但我没有一个环境来测试它在目前所以ppared进行调试$ P $。

      Here's a snippet I threw together real quick, but I don't have an environment to test it in currently so be prepared to debug it.

      $fileData = fillArrayWithFileNodes( new DirectoryIterator( '/path/to/root/' ) );
      
      function fillArrayWithFileNodes( DirectoryIterator $dir )
      {
        $data = array();
        foreach ( $dir as $node )
        {
          if ( $node->isDir() && !$node->isDot() )
          {
            $data[$node->getFilename()] = fillArrayWithFileNodes( new DirectoryIterator( $node->getPathname() ) );
          }
          else if ( $node->isFile() )
          {
            $data[] = $node->getFilename();
          }
        }
        return $data;
      }
      

      这篇关于目录结构的深层递归数组在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆