How to sort this array?

Discussion in 'PHP' started by dadougalee, Nov 29, 2007.

  1. #1
    Hi everyone. I have an array of arrays that contain a users name, email, website, and the number of occurrences.

    To give you a better idea, it looks something like this:

    $top_commentors= array();

    $commentor= array("Name"=>"dummy", "Email"=>"dummy", "Website"=>"dummy.com", "Count"=>3);

    $commentor's get pushed into $top_commentors.

    My question is, once I have this array of $top_commentors that contains this info, how can I sort it so that the commentor with the highest count will be at the top?

    Thanks for any help!
     
    dadougalee, Nov 29, 2007 IP
  2. Kwaku

    Kwaku Well-Known Member

    Messages:
    1,217
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Didn't test, but something like this;

    <?
    $top_commentors =array( array("Name"=>"dummy", "Email"=>"dummy",
    "Website"=>"dummy.com", "Count"=>3),
    array("Name"=>"dummy", "Email"=>"dummy2",
    "Website"=>"dummy.com", "Count"=>1),
    array("Name"=>"dummy", "Email"=>"dummy3",
    "Website"=>"dummy.com", "Count"=>10));

    $count=array();
    foreach($top_commentors as $k=>$v) {
    $count[$k] = $v['Count'];
    }

    array_multisort($count, SORT_DESC, SORT_NUMERIC, $top_commentors);

    print_r($top_commentors);

    ?>
     
    Kwaku, Nov 29, 2007 IP
  3. dadougalee

    dadougalee Peon

    Messages:
    589
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Genius my man, it works! MUAH!
     
    dadougalee, Nov 29, 2007 IP