Help with best possible method:

Relatively new to php just wonder whats the most efficient way to go about this scenario:
I have a variable $x that can be between 0-9 based on what the api gives me.

Id like to display $x as a roman numeral based on what I get back, so would the best solution be to use a switch statement?

switch ($x) {
    case '0':
         $xFORMATTED = " I "
    case '1':
         $xFORMATTED = " II "
    case '2':
         $xFORMATTED = " III "

echo ($xFORMATTED)

PS. I have alot of variables similar to “x” in the statement above what would be the best way in converting them all to roman numerals based on the apis response.

Just create an array with the roman numerals as the key values and match to the API return.

Only problem is there is no zero in Roman Numerals so what are you going to do about that?

The api literally returns “x: 4” or “x: 9” etc… so not sure how I’d match an array to it?
Also the values I can recieve are 1-9 (made a mistake in previous post) so I dont have to worry about 0.

<?php declare (strict_types = 1);

$api = 3;

$arr = [1 => "I", 2 => "II", 3 => "III", 4 => "IV"];
echo "Roman Number $arr[$api]";
1 Like

Sounds like you want to handle some JSON?

Yeah its JSON why would I be able to do it differently to the solution provided above?

Sponsor our Newsletter | Privacy Policy | Terms of Service