array declared in one function, populated in nested function, remains blank





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















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

}









share|improve this question

























  • You have global $dir and $dir as a function parameter. Change the second one to another name.

    – Pyton
    Nov 25 '18 at 11:00






  • 2





    Where are you calling to recursiveScan in the first time?

    – dWinder
    Nov 25 '18 at 11:07











  • @david in the testing function I put $data=recursiveScan($dir);

    – jack
    Nov 25 '18 at 11:58











  • @pyton, didn't make any differnce

    – jack
    Nov 25 '18 at 11:59











  • @jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

    – user8713979
    Nov 25 '18 at 12:45


















0















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

}









share|improve this question

























  • You have global $dir and $dir as a function parameter. Change the second one to another name.

    – Pyton
    Nov 25 '18 at 11:00






  • 2





    Where are you calling to recursiveScan in the first time?

    – dWinder
    Nov 25 '18 at 11:07











  • @david in the testing function I put $data=recursiveScan($dir);

    – jack
    Nov 25 '18 at 11:58











  • @pyton, didn't make any differnce

    – jack
    Nov 25 '18 at 11:59











  • @jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

    – user8713979
    Nov 25 '18 at 12:45














0












0








0








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

}









share|improve this question
















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

}






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:15







jack

















asked Nov 25 '18 at 10:50









jackjack

125




125













  • You have global $dir and $dir as a function parameter. Change the second one to another name.

    – Pyton
    Nov 25 '18 at 11:00






  • 2





    Where are you calling to recursiveScan in the first time?

    – dWinder
    Nov 25 '18 at 11:07











  • @david in the testing function I put $data=recursiveScan($dir);

    – jack
    Nov 25 '18 at 11:58











  • @pyton, didn't make any differnce

    – jack
    Nov 25 '18 at 11:59











  • @jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

    – user8713979
    Nov 25 '18 at 12:45



















  • You have global $dir and $dir as a function parameter. Change the second one to another name.

    – Pyton
    Nov 25 '18 at 11:00






  • 2





    Where are you calling to recursiveScan in the first time?

    – dWinder
    Nov 25 '18 at 11:07











  • @david in the testing function I put $data=recursiveScan($dir);

    – jack
    Nov 25 '18 at 11:58











  • @pyton, didn't make any differnce

    – jack
    Nov 25 '18 at 11:59











  • @jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

    – user8713979
    Nov 25 '18 at 12:45

















You have global $dir and $dir as a function parameter. Change the second one to another name.

– Pyton
Nov 25 '18 at 11:00





You have global $dir and $dir as a function parameter. Change the second one to another name.

– Pyton
Nov 25 '18 at 11:00




2




2





Where are you calling to recursiveScan in the first time?

– dWinder
Nov 25 '18 at 11:07





Where are you calling to recursiveScan in the first time?

– dWinder
Nov 25 '18 at 11:07













@david in the testing function I put $data=recursiveScan($dir);

– jack
Nov 25 '18 at 11:58





@david in the testing function I put $data=recursiveScan($dir);

– jack
Nov 25 '18 at 11:58













@pyton, didn't make any differnce

– jack
Nov 25 '18 at 11:59





@pyton, didn't make any differnce

– jack
Nov 25 '18 at 11:59













@jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

– user8713979
Nov 25 '18 at 12:45





@jack does this ca3.php.net/manual/en/control-structures.alternative-syntax.php solve your problem?

– user8713979
Nov 25 '18 at 12:45












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466714%2farray-declared-in-one-function-populated-in-nested-function-remains-blank%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer




























      0














      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






      share|improve this answer


























        0












        0








        0







        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 12:47









        dWinderdWinder

        6,99131231




        6,99131231
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466714%2farray-declared-in-one-function-populated-in-nested-function-remains-blank%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud