[PHP] sorting 2-dimensional array by timestamp-key

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by bonzo meier, Feb 19, 2008.

  1. bonzo meier

    bonzo meier Member

    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    6
    hi,

    back again with some rss-newsbox-trouble...
    I wrote a script that collects items from different rss-newsfeeds into one array. this array is supposed to be sorted by date in order to have the newest rss-item on top.

    what I did is:
    Code:
    //get my news-items
    foreach ($rss->items as $item)
                 {    
                  $href = $item['link'];
                  $title = $item['title'];
                  $description = $item['description'];
    
           //get the publishing date from each item
           $datedata = explode(' ', $item['pubdate']);
    
          //turn the publishing date into a unix timestamp
          $date2sort = mktime(0,0,0, $datedata[2], $datedata[1], $datedata[3]); //datedata´s are month, day, year
    
          //collect this item into an array
          $thisitem = array($title, $description, $href);
    
          //...and add this one to my complete-news-array with the timestamp as key
                $rss_complete[$date2sort] = $thisitem;
    }
    
    //then I try sorting the $rss_complete by keys
    
    uksort ($rss_complete, "sort_by_size"); // <- AND THIS DOESN´T WORK; NEITHER DOES ksort()/krsort();
    
    //oh, my sorting function i use in uksort (in the original script it is provided before I perform the uksort())
    
    function sort_by_size($a, $b)
            {
            if($a < $b) return 1;
            if($a > $b) return -1;
            return 0;
            }
    
    
    what´s happening is that the first item in the array (after sorting) is one of today, the second one is one from yesterday (although there are about 27 items with today´s date, i can see that by print_r($rss_complete) easily) and the third one is more than one week old. after that there´s one from yesterday again. so obviously there is not much sorting done here.

    how come that? any ideas?
    i truly hope that.

    peace, bonzo


    NO replies?
    ok, maybe someone might need this sometime:

    the problem was in

    mktime(0,0,0, $datedata[2], $datedata[1], $datedata[3]);

    ...because like this ALL items of today get the same key and the sort_by_size($a, $b) function would return 0, so uksort() will pick the next lower key, whis is yesterday´s timestamp...

    i solved it by adding $itemcount = 0; outside the foreach ($rss->items as $item) and having $itemcount++ inside;
    then the 'timestamp keys' went
    $date2sort = mktime(0,0,0, $datedata[2], $datedata[1], $datedata[3]).$itemcount;

    now every key is unique and everything is working smoothly.

    peace,
    bonzo

    man, i´d donate to myself if i could but it says 'Invalid User Name Specified'

    peace,
    bonzo
     
    Last edited: Feb 20, 2008

Share This Page