SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: array_reverse

  1. #1
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question array_reverse

    I am making a small newspage that can be updated by php. But I want the latest post in the top and not in the bottom as it arrive now.
    How can I make my code doing what I want?

    I tried ex.

    $myfile = array_reverse ($myfile);

    anybody help me?

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isn't an advanced topic to discuss but since you've just gone and posted the thread here, and I notice this is your first thread (welcome), exactly what is

    PHP Code:
    $myfile ... // an array? I don't really know 
    Can you post the rest of the script? And what if any errors are you getting... Put this at the top of the file,

    PHP Code:
    error_reportingE_ALL ); 
    Which reports all errors, including notices

  3. #3
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll want to sort the output using MySQL, when you do your query, not using PHP.

  4. #4
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get $myfile as an array.. thats for sure.. But how do I manage to reverse it.. post by post... identifying by $line_num?

    Somebody?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's good, now how do you generate or build the array? What is the array structure? Contents? The function works on assocc., ie

    PHP Code:
    array( 'key' => 'one''key' => 'two' ... etc ); 
    If your using integers instead, then the function is not for you... Have you looked at the PHP manual? You've not posted enough to go on

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Moved to a more appropriate forum. See Where should I post my thread? for details

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Zealot ceefour's Avatar
    Join Date
    Feb 2005
    Location
    Bandung, Indonesia
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all things like this should be handled by the database, using the ORDER BY clause. And you can do even more, using WHERE, LIMIT, etc.

    But if you want to just reverse the array, well array_reverse is the way to do it.

    If you want to sort it, try sort, asort, ksort, or any of the reverse functions, rsort, arsort, krsort.

    If you need additional features, try the user-function versions of these functions, namely usort, uasort, uksort, etc.

    Hope this helps a bit.


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
  •