Sorting and Limiting Results
I haven't been able to get this one to work properly. Basically someone takes a test. Each question has 5 possible answers and is given a value of 1-5. Then the results are added up like so:
Q1+Q37+Q73 = $somevar so if someone chose Very Much (value of 5) for all 3 questions, $somevar = 5+5+5 = 15
There are 18 different categories that need to be added up like this. Once this categories are all added up, they are added to an associative array and given key/values. Something like:
$array = ("key1"=>$somevar,
"key2"=>$somevar2,
etc. on up to 18);
That array is then sorted in reverse order with the top score at the beginning. That works fine.
arsort($array);
What I am having problems with is how to limit the results to the top 5 categories. Any ideas on this one?
Mark Irving
I have a mind like a steel trap; it is rusty and illegal in 47 states
Mark Hensler posted this at 08:13 — 16th January 2004.
He has: 4,048 posts
Joined: Aug 2000
You just want the top 5 elements of the associative array?
<?php
$top5 = array_slice($array, 0, 5);
?>
mairving posted this at 13:15 — 16th January 2004.
They have: 2,256 posts
Joined: Feb 2001
You know I knew that it wasn't something easy but I couldn't seem to come up with it. Thanks, bud.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.