Page 1 of 1

Call what is to print from the array ?

Posted: Thu Apr 09, 2009 2:07 pm
by Peuplarchie
Good day to you all,
the following code read a directory recurrently and put the files and folders in an array and I print the array.

I wonder if there is a way to choose which part of the array to print

Ex : print_r(recur_dir('Art')['level' = 1]['name']);

Here is my code

Code: Select all

 
 
 
 
<?php
 
session_start(); 
$dir = "Art/";
   function recur_dir($dir)
   {
       $dirlist = opendir($dir);
       while ($file = readdir ($dirlist))
       {
           if ($file != '.' && $file != '..')
           {
               $newpath = $dir.'/'.$file;
               $level = explode('/',$newpath);
               if (is_dir($newpath))
               {
                   $mod_array[] = array(
                           'level'=>count($level)-1,
                           'path'=>$newpath,
                           'folder'=>$dir,      
                           'name'=>end($level),
                           'kind'=>'dir',
                           'mod_time'=>date ("D F dS Y H:i:s A ", filemtime($newpath)),
                           'content'=>recur_dir($newpath) );
               }else{ 
                   $mod_array[] = array(
                           'level'=>count($level)-1,
                           'path'=>$newpath,
                           'folder'=>$dir,   
                           'name'=>end($level),
                           'kind'=>'file',
                           'mod_time'=>date ("D F dS Y H:i:s A ", filemtime($newpath)),
                           'size'=>filesize($newpath) );
       }
   
    
           }
       }
       closedir($dirlist);
       $_SESSION['listimages']=$mod_array; 
       return $mod_array;
    
   }
   
 
?>
<html>
<head></head>
<body>
 
 
 
<?PHP
   echo '<pre>';
   print_r(recur_dir('Art'));
   echo '</pre>';
?>
 
 
</body>
</html>
 
 

Thanks !

Re: Call what is to print from the array ?

Posted: Thu Apr 09, 2009 3:01 pm
by John Cartwright
You cannot reference a function call as an array, you instead need to assign the returned array as a variable, and then you may access the array as you wish.

Code: Select all

$array = recur_dir('Art');
 
print_r($array['some_key']);