dqy006150 2018-11-25 10:50
浏览 95
已采纳

在一个函数中声明的数组(在嵌套函数中填充)保持空白

I've written the below function, but fileList is blank when echoed out. Any ideas why this might be happening? and how to fix it?

function testing($dir){
echo $dir;
$fileList=array();

    function recursiveScan($dir) {

        $tree = glob(rtrim($dir, '/') . '/*');
        if (is_array($tree)) {
            foreach($tree as $file) {
                if (is_dir($file)) {
                    echo $file . '('.filemtime($file).')'.'<br/>';
                    recursiveScan($file);
                    $fileList[date('YmdHis',filemtime($file))]=$file;
                } elseif (is_file($file)) {
                    echo $file . '('.filemtime($file).')'.'<br/>';
                    $fileList[date('YmdHis',filemtime($file))]=$file;

                }
            }
        ?>
<pre>
<?php print_r($fileList);?>
</pre>
<?php   

        }

    }
}

EDIT:

If I move the print_r bit of the code below up a few } then it outputs... but I want to output it once all directories have been searched through.

function recursiveScan($dir) {

        $tree = glob(rtrim($dir, '/') . '/*');
        if (is_array($tree)) {
            foreach($tree as $file) {
                if (is_dir($file)) {
                    echo $file . '('.filemtime($file).')'.'<br/>';
                    recursiveScan($file);
                    $fileList[date('YmdHis',filemtime($file))]=$file;
                } elseif (is_file($file)) {
                    echo $file . '('.filemtime($file).')'.'<br/>';
                    $fileList[date('YmdHis',filemtime($file))]=$file;

                }
            }
        }
    ?>
    <pre>
    <?php print_r($fileList);?>
    </pre>
    <?php

}
  • 写回答

1条回答 默认 最新

  • dongxi1879 2018-11-25 12:47
    关注

    I think better approach will be to use the return value to function argument to get the results. Consider the following function:

    function recursiveScan($dir, $fileList) {
            $tree = glob(rtrim($dir, '/') . '/*');
            if (is_array($tree)) {
                foreach($tree as $file) {
                    if (is_dir($file)) {
                        $fileList = recursiveScan($file, $fileList);
                    } elseif (is_file($file)) {
                        $fileList[date('YmdHis',filemtime($file))]=$file;
                    }
                }
            }
            return $fileList;
    }
    

    Now you can trigger the first call by doing something like:

    $dir = "/"; // or from argument
    $fileList = recursiveScan($dir, array());
    

    After that, the $fileList will contain list of the files

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧