php - 如何将目录结构转换为 url 数组

标签 php arrays directory-structure

我想将我的目录结构转换为带有文件 URL 的数组格式。 这是我的目录结构。

public
  |-product_001
      |-documents
      |   |- doc_001.txt
      |   |- doc_002.txt
      |   |- doc_003.txt
      |
      |-gallery
          |- img_001.png
          |- img_002.png
          |- img_003.png

这就是我想要的:

array(
  'product_001' =>array(
      'documents' =>array(
         0 => "public/product_001/documents/doc_001.txt",
         1 => "public/product_001/documents/doc_002.txt",
         2 => "public/product_001/documents/doc_003.txt"
      )
      'gallery' =>array(
         0 => "public/product_001/gallery/img_001.png",
         1 => "public/product_001/gallery/img_002.png",
         2 => "public/product_001/gallery/img_003.png"
      )
  )
)

这里是函数:

function dirToArray($dir,$url) {

    $result = array();

    $cdir = scandir($dir);
    foreach ($cdir as $key => $value) {

        if (!in_array($value, array(".", ".."))) {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
                $url.=DIRECTORY_SEPARATOR.$value;
                $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value,$url);
            } else {
                $result[] = $url.DIRECTORY_SEPARATOR.$value;
            }
        }
    }

    return $result;
}

这是我到现在为止得到的输出:

Array
(
    [product_001] => Array
        (
            [documents] => Array
                (
                    [0] => public/product_001/documents/doc_001.txt
                    [1] => public/product_001/documents/doc_002.txt
                    [2] => public/product_001/documents/doc_003.txt
                )

            [gallery] => Array
                (
               [0] => public/product_001/documents/gallery/img_001.png
               [1] => public/product_001/documents/gallery/img_002.png
               [2] => public/product_001/documents/gallery/img_003.png
                )

        )

)

任何人都可以帮助我实现这一目标吗? 非常感谢。

最佳答案

应该更容易。 通常,如果您有递归,则不需要状态。 因此,只需阅读您的 $url 并清理代码,无需多次进行连接。

根据 Ryan Vincents 的 评论添加了动态分隔符。

根据 Mavericks 的 注释添加了根参数。

<?php

function dirToArray($dir, $separator = DIRECTORY_SEPARATOR, $root = '') {

    $result = array();
    if ($root === '') {
        $root = $dir;
    }

    $cdir = scandir($dir);
    foreach ($cdir as $key => $value) {

        if (!in_array($value, array(".", ".."))) {
            $current = $dir . $separator . $value;

            if (is_dir($current)) {
                $result[$value] = dirToArray($current, $separator, $root);
            } else {
                $result[] = str_replace($root, '',$current);
            }
        }
    }

    return $result;
}

关于php - 如何将目录结构转换为 url 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359138/

相关文章:

macos - 如何在 Mac OS X 上使用/home 目录

php - NGINX/PHP/MySQL 非英文字符位置错误

php - 通过另一个数组对数组进行分组?

php - 在 39 个结果后删除 txt 文件中的数据

java - 当 JSONArray 给出空值时显示文本 - android

python - Numpy:如何将(N,)维向量插入(N,M,D)维数组作为沿D轴的新元素? (N、M、D+1)

php - 在我的webapp中存储异常?

php - 在将图像发送到服务器之前压缩图像

c - 将字符分配给在 C 中使用 malloc 声明的二维数组时收到警告

Java : Display list of directories and files on the server through a web page