user3294481
user3294481

Reputation: 27

Get parent key of array in a recursive function in PHP

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 ?

Upvotes: 0

Views: 781

Answers (2)

ZiupeX
ZiupeX

Reputation: 338

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
 )

Upvotes: 1

Tularis
Tularis

Reputation: 1506

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;
}

Upvotes: 2

Related Questions