ksort ($votes);
foreach ($votes as $total => $contestant){
$ordervotes[]= $contestant;
}
echo "<li> And the winner is: {$ordervotes[4]}</li>";
echo "<li> And the loser is: {$ordervotes[0]}</li>";
echo "<li> {$ordervotes[1]} came second last</li>";
This works fine when none of the ‘$total’s are the same, if they are the same i get an error code. I realise I could use the ‘max/min’ to get the first and last elements of the array, but how do i go about finding the second last?
Thank you
Joe
Why don’t you try:
You also don’t need to populate another array with the same values – you can keep them in
$votes. You might also want to look into sorting your array by value instead of by key (which I assume you’re trying to do).If you’re expecting duplicate keys, you need to remodel the way you’re storing your data. Consider using a multidimensional array:
You will be able to sort this array using this function and code: