<?php
function repeatInt(int $val, int $min, int $max)
{
$listLength = $max - $min + 1;
$key = ($val % $listLength) % $listLength;
if ($key < 0) {
$key = $listLength + ($key % $listLength);
}
return $key;
}
echo 'Test 2 to 5' . PHP_EOL;
echo '---------------------------------' . PHP_EOL;
foreach (range(-10, 10) as $i)
{
echo $i . ' → ' . repeatInt($i, 2, 5) . PHP_EOL;
}
echo PHP_EOL;
echo 'Test 3 to 8' . PHP_EOL;
echo '---------------------------------' . PHP_EOL;
foreach (range(-10, 10) as $i)
{
echo $i . ' → ' . repeatInt($i, 3, 8) . PHP_EOL;
}
echo PHP_EOL;
echo 'Test -2 to -4' . PHP_EOL;
echo '---------------------------------' . PHP_EOL;
foreach (range(-10, 10) as $i)
{
echo $i . ' → ' . repeatInt($i, -4, -2) . PHP_EOL;
}
echo PHP_EOL;
preferences:
32.34 ms | 402 KiB | 5 Q