【问题标题】:Array Associate Cannot Pass All On Function When Associate Already Created已创建关联时,数组关联无法传递所有函数
【发布时间】:2018-09-14 20:45:12
【问题描述】:

我的目录结构如下图。

C:\xampp\htdocs\rosoka\file
----- 2018-03-02
-------------test1.json
-------------test2.json
----- 2018-03-03
-------------test3.json
-------------test4.json

我希望我的所有文件夹都成为函数外最后一个跟踪上这样关联的数组的键

Array ( [C:/xampp/htdocs/rosoka/file] => Array ( )) 
Array ( [C:/xampp/htdocs/rosoka/file/2018-03-02] => Array ()) 
Array ( [C:/xampp/htdocs/rosoka/file/2018-03-03] => Array ()) 

这是我的源代码请帮帮我。

<?php
    $no_entity = array();
    listFolderFiles("C:/xampp/htdocs/rosoka/file", $no_entity);
function listFolderFiles($dir, $no_entity){
        echo "<br>";
    if (array_key_exists($dir, $no_entity)) {
        echo "why no result here??? T_T";
    } else {
        $no_entity[$dir] = array();
        print_r($no_entity);
    }
    $real_path = "C:/xampp/htdocs/rosoka/file";
    $real_path =  str_replace($real_path,"",$dir);
    set_time_limit (3000000);
    ini_set('memory_limit', '-1');
    $ffs = scandir($dir);
    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);
    $ffs = array_values($ffs);
    if (count($ffs) < 1)
        return;
    foreach($ffs as $ff){
       if(is_dir($dir.'/'.$ff)) {
            listFolderFiles($dir.'/'.$ff, $no_entity); 
        } else { 
        }
    }
}
    //  "WHERE MY ARRAY T_T";
    var_dump($no_entity);   
?>

(用于测试 ekstract 到 htdocs) https://drive.google.com/file/d/1dtXbQFrWoD0dQFOb-2YRqcH4eKPUTzZ9/view?usp=sharing

【问题讨论】:

标签: php xampp directory php-node


【解决方案1】:

如果没有更好的文件夹结构示例以及您想要从中得到什么,这是我最好的猜测。

这将获取在目录中找到的文件夹,并像您发布的那样创建一个数组。

$path = 'C:/xampp/htdocs/rosoka/file';
$dir = glob($path . '/*', GLOB_ONLYDIR);

foreach($dir as $d => $val){

  $results[][$val]  = array();

}

print_r($results);

【讨论】:

  • 是的,就像那个结果一样,但是使用循环函数先生,我将探索更多 glob() 并与我之前的代码 thx 结合
  • 很高兴它帮助了你。 Glob 对于获取文件夹和文件非常有用。
猜你喜欢
  • 2021-12-26
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 2011-02-02
相关资源
最近更新 更多