Jump to content

Ranking multi-dimensional array


SCook

Recommended Posts

Hi all,

 

Here is my problem:  I have a list of people who've taken a quiz, they have the number right, and their time.  I want to sort them all so that they're organized.

 

I was thinking of an array like:

my_array[0][0] = id;

my_array[0][1] = number_right;

my_array[0][1] = time;

 

Is there a way to sort the array by a specific key?  Seems like I should know this, but I'm blocked.  Any help would be great, thanks

Link to comment
https://forums.phpfreaks.com/topic/130638-ranking-multi-dimensional-array/
Share on other sites

just to elaborate, in case you're wondering how exactly to use array_multisort(), you'll need to extract the number_right:

 

<?php
$number_right = array();
foreach($my_array AS $key => $this_array)
{
  $number_right[$key] = $this_array[1];
}

array_multisort($number_right, SORT_DESC, $my_array);

echo '<pre>';
print_r($my_array);
?>

 

that's an adaptation of the example in the manual.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.