Page 1 of 1

Sorting array problem.

Posted: Mon Sep 03, 2007 12:25 am
by Peuplarchie
Good day to you all,
I'm trying to have an array sort. I have try many way, and stfw.

Can somebody help me to have the following code to sort the result folder first and file after their respective folder.

Code: Select all

<?PHP

   function recur_dir($dir)
   {
       $dirlist = opendir($dir);
       while ($file = readdir ($dirlist))
       {

           if ($file != '.' && $file != '..')
           {
               $newpath = $dir.'/'.$file;
               $newpath2 = substr($newpath, 0, -4);
               $newpath0 = basename($newpath2,".*");
               $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));
rsort($mod_array);
echo '<tr><td bgcolor="#663300">';
echo '<center><b class="whhite00">'.$newpath.'</b></center></td></tr>';
echo "\n\r";
               }else{
  
                   $mod_array[] = array(
                           'level'=>count($level)-1,
                           'path'=>$newpath,
                           'name'=>end($level),
                           'kind'=>'file',
                           'mod_time'=>filemtime($newpath),
                           'size'=>filesize($newpath));

echo '<tr><td bgcolor="#ffffff"  onClick="if(this.bgColor==\'#0000ff\'){this.bgColor=\'#ffffff\'}else{this.bgColor=\'#0000ff\'}">';
echo '<label for="'.$newpath0.'" style="padding-right:3px;display:block;"   >';
echo '<input name="checkbox[]" value="'.$newpath. '" type="checkbox" id="'.$newpath0. '" >';
echo '<img src="'.$newpath.'" width="100" ID="'.$newpath0.'" title="'.$newpath.'" alt="'.$newpath1.'"></label></td></tr>';
echo "\n\r";
              }

           }
       }
       closedir($dirlist);
       return $mod_array;
   }
   
   recur_dir('Images');




?>



Thanks !

Posted: Mon Sep 03, 2007 1:03 am
by s.dot
what do you want to sort by?

sort($array, SORT_STRING) [sort by name]
sort($array, SORT_NUMERIC) [sort by filesize]
etc..

You'll probably need to add the results in a container array during each iteration of the loop, if you want to sort multiple directories.