Multi-dimensional array sorting guidance needed...

Discussion in 'PHP' started by Peuplarchie, May 25, 2008.

  1. #1
    Good day to you,
    I have an multi-dimensional array and I need to sort it by date and then height.

    My question is how would I build my code to do such a function ?

    Here is how I have built my array :

    
    
    <?php
    
    
    $imgdir= $_GET['folder'];
    $imgdir.="/";
    
    
    
    function dircont($imgdir)
     {
         $dir_array = array();
         if (false !== ($dir = opendir($imgdir)))
         {
             while (false !== ($file = readdir($dir)))
             {
                 if ($file != '.' && $file != '..')
                 {
                     $fInfo = array();
                     $fInfo[] = $file;
                     if (($imageInfo = @getImageSize($imgdir."".$file)) !== false)
                     {
                         $fInfo['width'] = $imageInfo[0];
                         $fInfo['height'] = $imageInfo[1];
                     }
                     
                     if (($stat = stat($imgdir."".$file)) !== false)
                     {
                         $fInfo['mtime'] = $stat['mtime'];
                         // Lol, forgot to put the size in :P
                         $fInfo['size'] = $stat['size'];
                     }
                     
                     $dir_array[] = $fInfo;
                 }
             }
             return $dir_array;
         }
         else
         {
             return false;
         }
     }  
     
     
     
    
     // To print..
     echo "<pre>";
     print_r(dircont($imgdir));
     echo "</pre>";
    
    
    ?> 
    
    
    PHP:

     
    Peuplarchie, May 25, 2008 IP
  2. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try adding a first index which is a concatenation of the date time. For example $fInfo[date.'--'.time] and sort by that. You must make sure that the key is unique and that the length of each key is the same (pad with 0's if needed).
     
    TeraTask, May 25, 2008 IP
  3. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Can you detail a bit more please, i'm new to php ?
     
    Peuplarchie, May 25, 2008 IP
  4. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Don't have time to research all the functions and code something specific, but I whipped this up in a couple mins:

    
    <pre><?php
      $array_to_sort = array('1400'=> array(2=>"Yes", 1=>"No"),
                             '200'=> array(32=>"Maybe",33=>"Sure"),
                             '100'=> array(100=>"Good",50=>array("Bad","Really Bad"),75=>"Ugly"),
                            );
      $sorted_array = array();
      foreach ($array_to_sort as $first_dimension_index=>$second_dimension) {
        foreach($second_dimension as $second_dimension_index=>$dimensional_value) {
          $sorted_array[str_pad($first_dimension_index,5,'0',STR_PAD_LEFT).'--'.str_pad($second_dimension_index,5,'0',STR_PAD_LEFT)] = $dimensional_value;
        }
      }
      ksort($sorted_array);
      print_r($sorted_array);
    ?>
    
    PHP:
    That should give you enough of an idea to see what I mean.
     
    TeraTask, May 25, 2008 IP
  5. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    where about should i put such a code in mine ?
    I have tried many place changing foreach ($dir_array as $first_dimension_index=>$second_dimension) {


    
    
    
    <?php
    
    
    $imgdir= $_GET['folder'];
    $imgdir.="/";
    
    
    
    function dircont($imgdir)
     {
         $dir_array = array();
         if (false !== ($dir = opendir($imgdir)))
         {
             while (false !== ($file = readdir($dir)))
             {
                 if ($file != '.' && $file != '..')
                 {
                     $fInfo = array();
                     $fInfo[] = $file;
                     if (($imageInfo = @getImageSize($imgdir."".$file)) !== false)
                     {
                         $fInfo['width'] = $imageInfo[0];
                         $fInfo['height'] = $imageInfo[1];
                     }
                     
                     if (($stat = stat($imgdir."".$file)) !== false)
                     {
                         $fInfo['mtime'] = $stat['mtime'];
                         // Lol, forgot to put the size in :P
                         $fInfo['size'] = $stat['size'];
                         
                          
    
                     }
                       
    
    
      $sorted_array = array();
      foreach ($dir_array as $first_dimension_index=>$second_dimension) {
        foreach($second_dimension as $second_dimension_index=>$dimensional_value) {
          $sorted_array[str_pad($first_dimension_index,5,'0',STR_PAD_LEFT).'--'.str_pad($second_dimension_index,5,'0',STR_PAD_LEFT)] = $dimensional_value;
        }
      }
    
                     
                     $dir_array[] = $fInfo;
                 }
             }
             return $dir_array;
         }
    
    
      }
     
       ksort($sorted_array);
      print_r($sorted_array);
    
    
    ?> 
    
    
    
    
    PHP:
     
    Peuplarchie, May 26, 2008 IP
  6. TeraTask

    TeraTask Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oh, man, you missed the point. Read the code for the concept. I wasn't giving you something you could use directly. You'd have to study it first and then apply the concept. In forums, I try to teach. When I'm paid, I just do the work. Now, though, I'm tired, so outta here for tonight (and tomorrow, a holiday).
     
    TeraTask, May 26, 2008 IP
  7. Peuplarchie

    Peuplarchie Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Enjoy,
    thanks for your time, i'm working on it, I didn't thought so, but I thought that part fitted there , with my slow learning brain . I'm learning, i'm taking themost of my time working on project like that, I'll know !!!!!!!!!!

    Take good care and thanks !
     
    Peuplarchie, May 26, 2008 IP