Why store it like that?
The best way would be storing by unique names, since their names are all unique in your example:
PHP Code:
$students = array('John' => 59, 'Mary' => 42, 'Steve' => 72, 'Harry' => 31, 'Sally' => 59);
this is the same as doing:
PHP Code:
$students['John'] = 59;
$students['Mary'] = 42;
...
Then to sort, do
PHP Code:
arsort($students);
And that's it.
If you do want to keep it as a multidimensional array, then (rename your variable to $students[] it makes more sense this way)
PHP Code:
foreach($students as $key => $row)
{
$student[$key] = $row[0];
$results[$key] = $row[1];
}
array_multisort($results, SORT_DESC, $student, SORT_ASC, $students);
Basically the way we look at the information is important for working with multidimensional arrays, we have our main array, which holds all the students, it is built up of smaller arrays which contains the student and results hence my variable naming.
We first need to go through our array and store the information inside it in their own array, so storing the same information in it's own array, this way when sorting, we are still working with the same information (3 times as much), but that's what you asked for and this is the only way I know how we can do it.
It is quite confusing, but knowing that $row[0] is where our students are and $row[1] is where the results are (could have called it $col which may make it clearer for you, I just worked on what PHP output in print_r), we want to sort the results first, then students (if they have the same results, then we'd like the person with the lower alpha character to have higher precedance), then we tell it the main array so that it can update it to reflect the changes for the keys.
Before:
Code:
Array
(
[0] => Array
(
[0] => John
[1] => 59
)
[1] => Array
(
[0] => Mary
[1] => 42
)
[2] => Array
(
[0] => Steve
[1] => 72
)
[3] => Array
(
[0] => Harry
[1] => 31
)
[4] => Array
(
[0] => Sally
[1] => 59
)
)
After:
Code:
Array
(
[0] => Array
(
[0] => Steve
[1] => 72
)
[1] => Array
(
[0] => John
[1] => 59
)
[2] => Array
(
[0] => Sally
[1] => 59
)
[3] => Array
(
[0] => Mary
[1] => 42
)
[4] => Array
(
[0] => Harry
[1] => 31
)
)
Hopefully what I suggest helps you.
Cheers,
MC