Advanced Search

Results 1 to 3 of 3

Thread: php sort using associative array

  1. #1
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default php sort using associative array

    Hi,

    I'm using krsort, ksort, arsort and asort in a script. Fine all goes perfectly, except that the sort field is based on file names from the directory contents, and they are (historically) ugly, as in MMDDYY, not YYYYMMDD, causing the different years to be interspersed together. I've read the man page at http://us2.php.net/manual/en/function.sort.php, and quite can't grasp what to do here. My sort code is this:
    Code:
    if($_GET['sort'] == 'alpha'){
        if($_GET['mode'] == 'desc'){
            krsort($files);
            $highlight = 'alpha_desc';
        } else {
            ksort($files);
            $highlight = 'alpha_asc';
        }
    } else {
        if($_GET['mode'] == 'asc'){
            asort($files, SORT_NUMERIC);
            $highlight = 'date_asc';
        } else {
            arsort($files, SORT_NUMERIC);
            $highlight = 'date_desc';
        }
    }
    And my $files array is built like this:
    Code:
    $dir = ".";
    $directories = array();
    $files = array();
    $dir = (substr($dir, -1) == '/') ? substr($dir, 0, -1) : $dir;
    if(is_dir($dir)){
        if($handle = opendir($dir)){
            while(false !== ($file = readdir($handle))){
                if($file != "." && $file != ".."){
                    if(is_dir($dir.'/'.$file)){
                        $directories[$file] = filemtime($dir.'/'.$file);
                    } else {
                        if(strpos($file, '.rar') !== false) $files[$file] = filemtime($dir.'/'.$file);
                    }
                }
            }
            closedir($handle);
        } else {
            die('Could not open directory.');
        }
    } else {
        die('Invalid directory.');
    }
    Could someone explain what I need to do? I figure that I need to make a two dimensional array, say $associated, having three values for each row, one for the alpha sort key, one for the date sort key, and one for the file name. Then I could sort on either $associated[0], or $associated[1], but that doesn't seem to work.....

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Fine all goes perfectly, except that the sort field is based on file names from the directory contents, and they are (historically) ugly, as in MMDDYY, not YYYYMMDD, causing the different years to be interspersed together. I've read the man page at http://us2.php.net/manual/en/function.sort.php, and quite can't grasp what to do here.
    You probably want uksort().
    Code:
    function parseMDY($date) {
      $month = substr($date, 0, 2);
      $day = substr($date, 2, 2);
      $year = substr($date, 4);
      return array(
        'm' => $month,
        'd' => $day,
        'y' => $year
      );
    }
    
    function compareMDY($a, $b) {
      if($a == $b)
        return 0;
    
      $da = parseMDY($a);
      $db = parseMDY($b);
      $ret = 0;
    
      foreach(array('d', 'm', 'y') as $k)
        if($da[$k] < $db[$k])
          $ret = -1;
        else if($da[$k] > $db[$k])
          $ret = 1;
      return $ret;
    }
    To sort an array on its keys by an MMDDYY or MMDDYYYY date, pass the latter of the above functions to uksort():
    Code:
    uksort($myArray, "compareMDY");
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    223
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    Ahhh! Works like a charm. I have never used this function before, so this is great. I just added a reverse sort function and a few more sort variables for the rest of the key.

    BTW, it's interesting to note that the array item order in the sort function is reverse from the intuitive
    Code:
    foreach(array('d', 'm', 'y') as $k)
    sorts with 'y' being the most significant, and 'd' the least.

    Thanks Twey, you are great.
    Last edited by Strangeplant; 01-25-2007 at 07:35 PM.

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
  •