SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Echo matching key in multidimention array

    Good day to you all,
    I'm working on a piece of code which read a directory recursively and list them as an array.

    What I'm trying to do here is to echo only the ones with dir as "dir" as 4th key.

    Can somebody help me i'm fairly new with arry, specificly multidimensional array.

    Here is my code :
    PHP Code:

    <?php
       $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,
                               
    'name'=>end($level),
                               
    'kind'=>'dir',
                               
    'mod_time'=>filemtime($newpath),
                               
    'content'=>recur_dir($newpath) );
                   }else{ 
                       
    $mod_array[] = array(
                               
    'level'=>count($level)-1,
                               
    'path'=>$newpath,
                               
    'name'=>end($level),
                               
    'kind'=>'file',
                               
    'mod_time'=>filemtime($newpath),
                               
    'size'=>filesize($newpath) );
                  }
               }
           }
           
    closedir($dirlist);
           
       
    ksort($mod_array);   
           return 
    $mod_array;
        
    $key array_search('dir'$mod_array);    

       echo 
    '<pre>';
       echo 
    $key;
       echo 
    '</pre>';
       
       }
       

    ?>
    Thanks !

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    While I'm not sure I can answer your question, I do know I can make your recursive file search better.

    PHP Code:
    <?php

    function recur_dir $path )
    {
        
    $path preg_replace'~[\\\\/]+~''/'realpath$path ) . '//' );
        if ( (bool)
    $path === false || !file_exists$path ) ) {
            
    trigger_error'Failed to find directory'E_USER_ERROR );
            return 
    false;
        }

        
    $files = array();

        
    $items glob$path '*' );
        foreach ( 
    $items as $item ) {

            if ( 
    is_dir$item ) ) {

                
    $files[] = array(
                    
    'level' => substr_count$item'/' ) - 1
                  
    'path' => $item
                  
    'name' => basename$item )
                  , 
    'kind'=>'dir'
                  
    'mod_time' => filemtime$item )
                  , 
    'content' => call_user_func__FUNCTION__$item )
                );
                continue;

            }

            
    $files[] = array(
                
    'level' => substr_count$item'/' ) - 1
              
    'path' => $item
              
    'name' => basename$item )
              , 
    'kind'=>'file'
              
    'mod_time' => filemtime$item )
              , 
    'size' => filesize$item )
            );

        }

        return 
    $files;
    }

    $files recur_dir'C:\Users\Logic\Pictures\Wallpaper' );
    foreach ( 
    $files as $file ) {
        if ( 
    $file['kind'] !== 'dir' ) continue;
        echo 
    $file['name']; // Print directory name.
    }
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    While I'm not sure I can answer your question, I do know I can make your recursive file search better.

    PHP Code:
    <?php

    function recur_dir $path )
    {
        
    $path preg_replace'~[\\\\/]+~''/'realpath$path ) . '//' );
        if ( (bool)
    $path === false || !file_exists$path ) ) {
            
    trigger_error'Failed to find directory'E_USER_ERROR );
            return 
    false;
        }

        
    $files = array();

        
    $items glob$path '*' );
        foreach ( 
    $items as $item ) {

            if ( 
    is_dir$item ) ) {

                
    $files[] = array(
                    
    'level' => substr_count$item'/' ) - 1
                  
    'path' => $item
                  
    'name' => basename$item )
                  , 
    'kind'=>'dir'
                  
    'mod_time' => filemtime$item )
                  , 
    'content' => call_user_func__FUNCTION__$item )
                );
                continue;

            }

            
    $files[] = array(
                
    'level' => substr_count$item'/' ) - 1
              
    'path' => $item
              
    'name' => basename$item )
              , 
    'kind'=>'file'
              
    'mod_time' => filemtime$item )
              , 
    'size' => filesize$item )
            );

        }

        return 
    $files;
    }

    $files recur_dir'C:\Users\Logic\Pictures\Wallpaper' );
    foreach ( 
    $files as $file ) {
        if ( 
    $file['kind'] !== 'dir' ) continue;
        echo 
    $file['name']; // Print directory name.
    }
    Thanks !

    Doesn't give me any errors, but it doesn't display anything either ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •