Grow your CSS skills. Land your dream job.

[Solved] Reverse order of array

  • # March 27, 2010 at 8:54 pm

    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:

    Code:
    < ?php
    print_r($dateArr);
    array_reverse($dateArr);
    print_r($dateArr);
    ?>
    Code:
    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?

    # March 27, 2010 at 9:25 pm

    How about this:

    Code:
    < ?php
    print_r($dateArr);
    $dateArr = array_reverse($dateArr);
    print_r($dateArr);
    ?>
    # March 27, 2010 at 10:16 pm

    That was it. Thanks.

    # April 2, 2010 at 9:55 pm

    I realise this was solved already, but wouldn’t it have been more efficient to sort the replies when executing the query?

    Code:
    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)?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".