note 46987 deleted from language.operators.array by googleguy

From: Date: Fri, 11 Jan 2013 09:27:47 +0000
Subject: note 46987 deleted from language.operators.array by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192911@lists.php.net to get a copy of this message
Note Submitter: Peter ---- The code from texbungalow at web dot de below is slightly incorrect. If my memory from primary school history is correct, roman numerals don't allow things like MIM - it has to be MCMXCIX, ie each step is only 1 level down (sorry, I can't explain it very well. a print_r($segments) comparing the snippets should explain. Corrected code: <?php function roman ($nr ) { $base_digits= array ( 1=> "I", 10=> "X", 100=> "C", 1000=> "M", ); $help_digits= array ( 5=> "V", 50=> "L", 500=> "D", ); $all_digits= $base_digits+ $help_digits; foreach ($base_digits as $key1=> $value1 ) foreach ($all_digits as $key2=> $value2 ) if ($key1< $key2 && $key1 >= ($key2 / 10)) $segments[$key2- $key1 ]= $value1. $value2; $segments+= $all_digits; krsort ($segments ); foreach ($segments as $key=> $value ) while ($key<= $nr ) { $nr-= $key; $str.= $value; } return $str; } echo roman (1998); // prints MCMXCVIII ?>

« previous php.notes (#192911) next »