0

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?

2 Answers2

0

I think this is what you want, couple of tweaks, wrote an array to collect the values, then just imploded the array with a a comma and added the full stop

 <?php

$winners = range(1, 100); // scaleable winners of /r/aww contest

$winners = array_combine($winners, $winners);//match keys to values

$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;
            }

            $out[]=$place;// echo as format "1st, 2nd, 3rd, 4th, (etc to end), 100th." end with period (need fixing)

    }

    echo implode(', ',$out).'.';

demo: http://ideone.com/RgDs9N

0

Here's how I would do it:

<?php

$mywinner = 0;
$winners = range(1, 100); // Start at 1 to avoid having to avoid 0.
$mywinner = array_rand($winners);

echo $mywinner . "<br>\n";
unset($winners[$mywinner - 1]); // Subtract 1 since we started at 1.

// Function to get the ordinal of a number.
function getOrdinal($number)
{
    // If this is 11-13, or 111-113, etc., use 'th'.
    if (($number % 100) >= 11 && ($number % 100) <= 13) {
        return sprintf('%dth', $number);
    }

    // Suffixes for each one's place.
    $endings = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th'];
    return sprintf('%d%s', $number, $endings[($number % 10)]);
}

// The array_map() here will run getOrdinal() on each of the winners,
// and return it as a new array, which we then implode.
echo implode(', ', array_map('getOrdinal', $winners)) . ".\n";

My output:

3<br>
1st, 2nd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 40th, 41st, 42nd, 43rd, 44th, 45th, 46th, 47th, 48th, 49th, 50th, 51st, 52nd, 53rd, 54th, 55th, 56th, 57th, 58th, 59th, 60th, 61st, 62nd, 63rd, 64th, 65th, 66th, 67th, 68th, 69th, 70th, 71st, 72nd, 73rd, 74th, 75th, 76th, 77th, 78th, 79th, 80th, 81st, 82nd, 83rd, 84th, 85th, 86th, 87th, 88th, 89th, 90th, 91st, 92nd, 93rd, 94th, 95th, 96th, 97th, 98th, 99th, 100th.
Will
  • 24,082
  • 14
  • 97
  • 108
  • This is more along the lines of what I would have liked my code to be, but I am not there yet. I'm having a hard time understanding how to use the % modulus. It just doesn't make sense to me yet. – theresthatguy May 22 '16 at 15:57