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

    Join Date
    Feb 2008
    Posts
    110
    Rep Power
    27

    Ordering an associative array


    I cannot seem to wrap my head around this logic. Maybe it's just too late at night.

    Anywho, I have a list of people with their names and ages. I want to associate their name with their age (similar to a db, but in an array) and then I want to order them by age (asc or desc, doesn't matter) and print their names and ages in that order.

    How would I go about doing that? I'm thinking I could start out with something like below. Does it make sense? Then how would I order by age?

    PHP Code:
    $person[0]['name'] = 'bob';
    $person[0]['age'] = 20;

    $person[1]['name'] = 'george';
    $person[1]['age'] = 29;

    $person[2]['name'] = 'matt';
    $person[2]['age'] = 24
    Thanks for any help, I think it's just too late and if I slept it off it'd hit me.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,574
    Rep Power
    2341
    uasort

    Comments on this post

    • mbreezy disagrees : troll.
    • kicken agrees : not a troll.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Hi,

    Use usort() to sort the array with a custom comparison function:

    PHP Code:
    usort($person, function ($p1$p2) {return $p1['age'] - $p2['age'];}); 
    If you have an old PHP version (< 5.4), you need to define the comparison function in the usual way and then pass the name to the usort function as a string.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    110
    Rep Power
    27
    Dang, is 5.2.17 too old?

    Originally Posted by Jacques3
    Hi,

    Use usort() to sort the array with a custom comparison function:

    PHP Code:
    usort($person, function ($p1$p2) {return $p1['age'] - $p2['age'];}); 
    If you have an old PHP version (< 5.4), you need to define the comparison function in the usual way and then pass the name to the usort function as a string.
  8. #5
  9. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Originally Posted by mbreezy
    Dang, is 5.2.17 too old?
    It doesn't have anyonymous functions, which make things like this much easier and cleaner.

    Comments on this post

    • mbreezy agrees : Thanks man!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    110
    Rep Power
    27
    Awesome, worked like a charm. How about reversing it? I tried rsort, got an error. Still peeling through documentation.
    Originally Posted by Jacques3
    It doesn't have anyonymous functions, which make things like this much easier and cleaner.
  12. #7
  13. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,304
    Rep Power
    7173
    Just invert the return result of your callback function.

    Comments on this post

    • mbreezy agrees : Thanks!
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    110
    Rep Power
    27
    Originally Posted by E-Oreo
    Just invert the return result of your callback function.
    Exactly what I did. Thanks a lot guys, I really appreciate the help.
  16. #9
  17. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    0
    Originally Posted by mbreezy
    Awesome, worked like a charm. How about reversing it? I tried rsort, got an error. Still peeling through documentation.
    The custom sorting functions don't have an "r" variant, but you can simply revert the order in the comparison function:

    PHP Code:
    usort($person, function ($p1$p2) {return $p2['age'] - $p1['age'];}); 

IMN logo majestic logo threadwatch logo seochat tools logo