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 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路