SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Array Sort

Hybrid View

  1. #1
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Sort

    Hi,

    I've a pretty simple problem, i am getting data in the form of two arrays and merging those two arrays and getting a result like:

    Array
    (
    [0] => Array
    (
    [0] => 246
    [group_id] => 246
    [1] => 2009-02-15 18:14:31
    [walldate] => 2009-02-15 18:14:31
    )

    [1] => Array
    (
    [0] => 193
    [group_id] => 193
    [1] => 2009-02-15 18:07:59
    [walldate] => 2009-02-16 18:07:59
    )

    [2] => Array
    (
    [0] => 318
    [group_id] => 318
    [1] => 2009-02-13 16:57:55
    [join_date] => 2009-02-13 16:57:55
    )


    [3] => Array
    (
    [0] => 246
    [group_id] => 246
    [1] => 2009-02-17 18:37:10
    [join_date] => 2009-02-17 18:37:10
    )

    )

    Now, what i want to do is to sort that array corresponding to date so that i have group_id with highest walldate or join_date at top, i am not somehow finding a way to do this.

    Please suggest me a way to achieve this.
    Thanks in advance,
    kul.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function cmp_date($a$b) {
        return 
    strtotime($a[1]) - strtotime($b[1]);
    }

    usort($array'cmp_date'); 

  3. #3
    SitePoint Addict kuldeepkaundal's Avatar
    Join Date
    Oct 2008
    Location
    India
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, but i didn't get it, how can i use it in context to my arrays?

    Will u plz explain it a bit?

    I tried your code, but it didn't work!

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should work. Post a sample of your array using var_export(), and use bbcode to format your forum post eg [code] or [php] tags.

    You can read the documentation for usort() to learn more about how it works.
    Off Topic:


    wait's patiently for lambda functions in php...

  5. #5
    SitePoint Enthusiast Technographs's Avatar
    Join Date
    Jan 2009
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Go by the sorting functions of Array. Check out php Manual at php dot net


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
  •