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

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

问题描述

我正在尝试将硬盘驱动器上的一些文件夹放入一个阵列中.

例如,假期照片.假设我们有这样的结构:

  • 第 1 组
    • 第 1 组第 1 项
    • 第 1 组第 2 项
    • 第 1 组的项目 ...
  • 第 2 组
    • 第 2 组的第 1 子集
      • 第 2 组第 1 子集的第 1 项
      • 第 2 组第 1 子集的项目...
    • 集 2 的子集 2
    • 随机文件,不是目录.
  • 第 3 组
  • ...

我想要这样的东西,作为一个数组.
这意味着我有 1 个大数组,并且该数组中有更多数组.每个集合和子集都有自己的数组.

我试图让它看起来像这样:

数组([设置 1] =>Array([0] => 第 1 组的第 1 项,[1] => 第 1 组的第 1 项,...)[设置 2] =>Array([Subnet 1] => Array([0] => Set 2 的 Subset 1 的 Item 1,[1] => ...), [Subnet 2] => Array([0] => ..., ..., ...), ..., [0] => 随机文件)[设置 3] =>大批(...)...)

我遇到了这个: 来构建你的数组

这是我很快拼凑起来的一个片段,但我目前没有一个环境可以测试它,所以准备调试它.

$fileData = fillArrayWithFileNodes( new DirectoryIterator( '/path/to/root/' ) );函数 fillArrayWithFileNodes( DirectoryIterator $dir ){$data = 数组();foreach ( $dir 作为 $node ){if ( $node->isDir() && !$node->isDot() ){$data[$node->getFilename()] = fillArrayWithFileNodes(new DirectoryIterator($node->getPathname()));}否则如果( $node->isFile() ){$data[] = $node->getFilename();}}返回 $data;}

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:

  • Set 1
    • Item 1 of Set 1
    • Item 2 of Set 1
    • Item ... of Set 1
  • Set 2
    • Subset 1 of Set 2
      • Item 1 of Subset 1 of Set 2
      • Item ... of Subset 1 of Set 2
    • Subset 2 of Set 2
    • Random file, not a dir.
  • Set 3
  • ...

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(...)
    ...
)

I came across this: 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...).

解决方案

I recommend using DirectoryIterator to build your array

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天全站免登陆