treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Reverse order of array

  • I'm trying to display posts in reverse order from newest to oldest. I've got the following array filled with timestamped dates:

    I'm using array_reverse to try to flip the order, but it doesn't seem to work:


    <?php
    print_r($dateArr);
    array_reverse($dateArr);
    print_r($dateArr);
    ?>


    			
    Array
    (
    [0] => 2010-03-27 15:43:31
    [1] => 2010-03-27 20:28:50
    [2] => 2010-03-27 20:52:28
    )
    Array
    (
    [0] => 2010-03-27 15:43:31
    [1] => 2010-03-27 20:28:50
    [2] => 2010-03-27 20:52:28
    )


    Am I not doing this right?
  • How about this:


    <?php
    print_r($dateArr);
    $dateArr = array_reverse($dateArr);
    print_r($dateArr);
    ?>
  • That was it. Thanks.
  • I realise this was solved already, but wouldn't it have been more efficient to sort the replies when executing the query?
    SELECT * FROM posts ORDER BY timestamp DESC


    That will select all the posts and order by the most recent timestamp (i.e. newest to oldest)?