Web Analytics Made Easy -
StatCounter Echo matching key in multidimention array - CodingForum

Announcement

Collapse
No announcement yet.

Echo matching key in multidimention array

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    you can use array_filer to extract dirs or files like this:
    PHP Code:
    function dir_filter($el){
       return (
    $el['kind'] == 'dir');
    }
    $dirs_array array_filter($mod_array,"dir_filter"); 
    http://www.php.net/manual/en/function.array-filter.php

    why don't you use two arrays, one for dirs and one for files?

    best regards

    Comment


    • #3
      Originally posted by oesxyl View Post
      you can use array_filer to extract dirs or files like this:
      PHP Code:
      function dir_filter($el){
         return (
      $el['kind'] == 'dir');
      }
      $dirs_array array_filter($mod_array,"dir_filter"); 
      http://www.php.net/manual/en/function.array-filter.php

      why don't you use two arrays, one for dirs and one for files?

      best regards


      Thanks !

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

      Comment


      • #4
        Originally posted by Peuplarchie View Post
        Thanks !

        Doesn't give me any errors, but it doesn't display anything either ?
        I guess is because you don't output anything. Try:
        PHP Code:
        print_r($dir_array); 
        best regards

        Comment

        Working...
        X