$nbRank = 3; $nbNumeric = 2; foreach (range(0, base_convert(str_pad('', $nbRank, $nbNumeric), $nbNumeric+1, 10)) as $i) { echo str_pad(base_convert($i, 10, $nbNumeric+1), 3, 0, STR_PAD_LEFT) . PHP_EOL; }
Simple idea. What you want is all numbers from 0 to X with the base $nbNumeric
, so we simply convert the maximum number to base 10, iterate over it using the usual 10 operational operators and convert them back to the base $nbNumeric
again.
Probably more readable, but actually exactly the same
$nbRank = 3; $nbNumeric = 2; // Top is "base_convert(222, 3, 10);" and therefore the upper limit $top = base_convert(str_pad('', $nbRank, $nbNumeric), $nbNumeric+1, 10); for ($i = 0; $i <= $top; $i++) { echo str_pad(base_convert($i, 10, $nbNumeric+1), 3, 0, STR_PAD_LEFT) . PHP_EOL; }
source share