I have an array from 0 to 100.
I need to append the proper suffix (st, nd, rd, th) to each and then display it as mentioned in the last comment IE "1st, 2nd, 3rd, 4th, 5th, (etc to last element)." <- Ends with "." instead of ", ".
Have been looking at similar style questions still cannot reach proper solution.
<?php
$winners = range(0, 100); // scaleable winners of /r/aww contest
// print_r($winners); // test above line
$last = count($winners)-1;
while (!$mywinner){ // ensure my Dog does not get 0th place
$mywinner = array_rand($winners); // randomly select my Dog's winning spot
}
echo $mywinner . '<br>'; // check my Dog's winning place to verify it's not being echoed below; comment this line upon completion
unset($winners[$mywinner]); // remove my Dog from list of other winners
// print out all winners except my dog
foreach ($winners as $place){ // run foreach loops on every winner to attach suffix
switch ($place){ //use switch to set what suffix to attach to winner's place
case 11: // make sure 11th place gets th suffix, not st
$place .= "th ";
break;
case 12:// make sure 12th place gets th suffix, not nd
$place .= "th ";
break;
case 13:// make sure 13th place gets th suffix, not 3rd
$place .= "th ";
break;
case substr($place, -1) == 1: // check places ending in 1 at append 'st' suffix
$place .= "st ";
break;
case substr($place, -1) == 2:// check places ending in 2 at append 'nd' suffix
$place .= "nd ";
break;
case substr($place, -1) == 3:// check places ending in 3 at append 'rd' suffix
$place .= "rd ";
break;
default: // default suffix for every other place
$place .= "th ";
break;
}
if ($place != 0){
echo (implode(", ", explode(" ", $place)));// echo as format "1st, 2nd, 3rd, 4th, (etc to end), 100th." end with period (need fixing)
}
}
?>
I'm so close. What am I overlooking here?