Call what is to print from the array ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Peuplarchie
Forum Contributor
Posts: 148
Joined: Sat Feb 04, 2006 10:49 pm

Call what is to print from the array ?

Post 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 !
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Call what is to print from the array ?

Post 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']);
Post Reply