#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    206
    Rep Power
    10

    Sorting a loop by date


    Hi,

    I'm using Simple XML to read from an XML file. I've done a loop for each event. It gives me all the events back in the order of the XML file. However, instead I am trying to sort events by date. Any idea how I could do this?

    Please see my code below. FYI, the date and time is in UTC timestamp format (eg. 1377361800)

    Thanks in advance for your help.

    PHP Code:
    $newresult $result->xpath("//match/matchname[.='$uniqueteam']/..");


       foreach (
    $newresult as $newmatchlist) {
        


    $newmatch trim($newmatchlist->{'matchname'}[0]);
    $sport $newmatchlist->{'sportname'}[0];
    $tournament $newmatchlist->{'tournamentname'}[0];
    $time=$newmatchlist->{'matchname'}[0]['dateandtime']; 


    Last edited by denhamd2; August 19th, 2013 at 03:56 AM.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    not sure what you're having trouble with. Grab the events, put them into an array and then then sort this array by the timestamps -- just like you would do with any other data.

    For example, you could use the timestamps as keys and then apply ksort().

    Comments on this post

    • Northie agrees : You got there first
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    PHP has some nice array sorters, consult the manual.

    As these are objects with properties....I'm not sure if there's anything in the SPL for this, or if you need to put them in an array first and then sort those
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo