You are trying to map the numbers 17 and 38 to the numbers 1 and 5, interpolating all values in between. Let us use these numbers in the below example where num represents any number from the range:
(num - 17) maps 17 and 38 to 0 and 21
(num - 17) / (21) maps 0 and 21 to 0 and 1
(num - 17) / (21) * (4) maps 0 and 1 to 0 and 4
(num - 17) / (21) * (4) + 1 maps 0 and 4 to 1 and 5
PHP Code:
function mapvaluetorange($array, $a, $b) {
$map = array();
$min = min($array);
$max = max($array);
foreach ($array as $value) {
$map[] = array(
"old" => $value,
"new" => ($value - $min) / ($max - $min) * ($b - $a) + $a
);
}
return $map;
}
$map = mapvaluetorange(array(34, 35, 33, 17, 38, 29, 31, 23), 1, 5);
Output:
int(34) -> float(4.2380952380952)
int(35) -> float(4.4285714285714)
int(33) -> float(4.047619047619)
int(17) -> int(1)
int(38) -> int(5)
int(29) -> float(3.2857142857143)
int(31) -> float(3.6666666666667)
int(23) -> float(2.1428571428571)
Use the round
function to round floats to integers.