【问题标题】:Use PHP to create a folder / file structure based off JSON that has been decoded使用 PHP 创建基于已解码的 JSON 的文件夹/文件结构
【发布时间】:2015-12-26 09:33:38
【问题描述】:

例如,我在下面有一个 build.json 文件。包含我在 JSON 中创建的基本文件夹/文件结构。

  {
    "folders": [
        {
            "name": "folder-a",
            "files": [
                {
                    "name": "file-a.html"
                },
                {
                    "name": "file-b.html"
                }
            ],
            "folders": [
                {
                    "name": "sub-folder-a",
                    "files": [
                        {
                            "name": "sub-file-a.html"
                        },
                        {
                            "name": "sub-file-b.html"
                        }
                    ]
                }
            ]
        },
        {
            "name": "folder-b",
            "files": [
                {
                    "name": "file-a.html"
                },
                {
                    "name": "file-b.html"
                }
            ]
        }
    ]
}

现在我在下面创建了简单的 PHP 代码,它可以循环遍历数组的第一部分。当然,如果我继续在第一个 foreach 中进行 foreach 循环,我可以继续遍历数组。问题是我不知道数组中有多少文件夹/文件。关于如何在不知道循环多少次的情况下继续循环的任何想法?谢谢!

$json = file_get_contents('build.json');

$decode = json_decode($json);

foreach($decode as $key => $val){
    foreach($val as $valKey => $data){
        var_dump($data);
    }
}

【问题讨论】:

  • 除非您知道文件夹的最大嵌套级别,否则您可能希望使用某种形式的递归。
  • 我不知道嵌套的级别。而且我在谷歌上搜索了很多关于递归的内容,只是不确定如何在循环中编写它。

标签: php arrays json loops recursion


【解决方案1】:

这是一个使用递归的工作脚本:

$json = file_get_contents('build.json');
$folders = json_decode($json);

function buildDirs($folders, $path = null){
  $path = $path == null ? "" : $path . "/";

  foreach($folders as $key => $val){
     mkdir($path.$val->name);
     echo "Folder: " . $path . $val->name . "<br>";

     if(!empty($val->files)){
        foreach($val->files as $file){
           //Create the files inside the current folder $val->name
           echo "File: " . $path . $val->name . "/" . $file->name . "<br>";
           file_put_contents($path . $val->name . "/". $file->name, "your data");
        }
     }

     if(!empty($val->folders)){ //If there are any sub folders, call buildDirs again!
        buildDirs($val->folders, $path . $val->name);
     }
  }
}

buildDirs($folders->folders); //Will build from current directory, otherwise send the path without trailing slash /var/www/here

记得给根文件夹设置正确的权限。

【讨论】:

  • 这在项目的根目录中创建了一个空文件夹-a。然后它也在项目的根目录下创建了file-a.html和file-b.html。
  • 我刚刚在我的文件夹中进行了测试,一切都很顺利。您使用的 JSON 与您发布的相同?
  • 致命错误:在第 19 行调用 /home/ubuntu/workspace/gen.php 中未定义的函数 buildDir() 调用堆栈:0.0003 253288 1. {main}() /home/ubuntu/workspace /gen.php:0 0.0004 261560 2. buildDirs() /home/ubuntu/workspace/gen.php:24 -- 也许与我的 php.ini 设置有关?
  • 我会尽量解释一下,我会在稍后更新我的答案。
【解决方案2】:

我没有测试过下面的代码,所以如果有错误请不要拍我,但它说明了使用递归函数解决此问题的基本过程。我们创建一个函数,它将在当前级别创建一个文件夹,并向其中添加任何必要的文件。如果它有子文件夹,它将调用自己,但将路径传递到新的子文件夹级别。当没有更多要处理的文件夹/文件时,它就会停止,因此您不应该面临无限递归(如无限循环)的危险。

/**
 * @param $path   string The base path in which $folder should be created
 * @param $folder object An object that contains the content to be saved
 */
function createFileStructure( $path, $folder ) {
    // create the current folder
    $path = $path . '/' . $folder->name;
    if ( mkdir( $path . $folder->name ) ) {
        foreach ( $folder->files as $file ) touch( $path . '/' . $file->name );
    }
    // now handle subfolders if necessary
    if ( isset( $folder->folders ) ) {
        createFileStructure( $path, $folder->folders );
    }
}

// now call the above recursive function
$path = __DIR__; // set the current directory as base
$json = file_get_contents('build.json');
createFileStructure( $path, json_decode( $json ) );

【讨论】:

  • 警告:mkdir(): File exists in /home/ubuntu/workspace/gen.php on line 10 -- 但文件夹/文件不在目录中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2020-04-14
  • 2012-07-20
  • 1970-01-01
  • 2012-02-11
相关资源
最近更新 更多