0

I have this array :

Array
(
    [0] => index.html
    [libraries] => Array
    (
        [0] => benchmark.html
        [1] => config.html
        [database] => Array
        (
              [0] => active_record.html
              [1] => binds.html
              [hey] => Array
              (
                   [0] => test.html
              )
        )
        [2] => email.html
    )
)

This is an array representing a tree of folders (arrays) and files (.html). I want to display on my page all the .html files with their path like :

index.html
libraries/benchmark.html
libraries/config.html
libraries/database/active_records.html
libraries/database/binds.html
libraries/database/hey/test.html
libraries/email.html

Here is my function but I don't achieve to display the parent key name:

private function map_dirs($dirs) {
    $dirs_list = array();
    $i = 0;
    $keys = array_keys($dirs);
    foreach($dirs as $dir)  {
        if(is_array($dir))
            $dirs_list = array_merge($dirs_list, $this->map_dirs($dir));
        else
            $dirs_list[] = $keys[$i].'/'.$dir;
        $i++;        
    }
    return $dirs_list;
}

Any idea ?

2 Answers 2

2

Well, add an optional key called $dirName to your function, and then also pass on the current-dir-name as the new param to the recursive part.

Like so:

public function map_dirs($dirs, $dirName='') {
   $dirs_list = array();
   foreach($dirs as $key=>$file) {
      if(is_array($file)) {
         $dirs_list = array_merge($dirs_list, $this->map_dirs($file, $dirName.$key.'/'));
      } else {
         $dirs_list[] = $dirName.$file;
      }
   }
   return $dirs_list;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I find another way using RecursiveIteratorIterator to list the files and their path both at a time : $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir_root), RecursiveIteratorIterator::SELF_FIRST); which one is better ?
well, RecursiveDirectoryIterator assumes that your directories really exist. The code you posted in your question (and that I adapted, per your request) does not assume the directories actually exist. So if you provide the input you showed in the question, this function would work, regardless of the server. While the RecursiveDirectoryIterator would only work if the files/folders actually exist on that server.
In fact, I was using the directory helper of CodeIgniter to list the directories and files so yes, they actually exist. Thank you for your help ! :)
1

From PHP 5 you can use recursiveDirectoryIterator and FileInfo object to get path and exacly extension of your html file. For example. In this case i preapare simulation with php extensions files but you can use html.

 function getPath($extension, $path){
     $directory = new \RecursiveDirectoryIterator($path);
     $iterator = new \RecursiveIteratorIterator($directory);
     $files = array();
     foreach ($iterator as $info) {
         $ext = $info->getExtension();
         if($ext === $extension){
             $files[$info->getBasename()] = $info->getPathname();
         }
     }
     return $files;
 }

 $array = getPath('php', __DIR__);
 echo '<pre>';
 print_r($array);
 echo '</pre>';

here is the output with php extension files:

Array
  (
     [index.php] => /var/www/system/framework/index.php
     [model_register.class.php] => /var/www/system/framework/sqls/mysql/model_register.class.php
     [model.login.php] => /var/www/system/framework/sqls/mysql/model.login.php
     [model_session.class.php] => /var/www/system/framework/sqls/mysql/model_session.class.php
     [model_logs.class.php] => /var/www/system/framework/sqls/mysql/model_logs.class.php
     [model_admin.class.php] => /var/www/system/framework/sqls/mysql/model_admin.class.php
 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.