I am basically trying to create a function that turns a Roman numeral into an integer.
I have an array:
$roman_numerals=[ 'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1 ];
I am new to PHP, so I'm still used to thinking like that, please keep in mind that I'm still involved :)
here is my function - or what I still have:
//Array function romanToInteger($key) { $roman_numerals=[ 'M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1 ]; $roman = intval($key); $result = 0; foreach ($roman_numerals as $key => $value) { while (strpos($roman, $key) === 0) { $result += $value; $roman = substr($roman, strlen($key)); } } var_dump($roman); //test echo $result; }
I was at this hour and would like him to see the light, any advice would be very grateful.
when i run it on the command line with
echo romanToInteger('I');
will i just go back to 0 and i think this has something to do with my intval?
Sorry again for being a noob, help evaluate though or any pointers! :)
function php foreach integer roman-numerals
Calvin taylor
source share