// floor of N = the largest element in the array smaller than or equal to N
function find_the_floor($arr, $N) {
$size = count($arr);
if ($N >= $arr[$size - 1]) {
return $size - 1;
}
if ($N < $arr[0]) {
return -1;
}
for ($i = 1; $i < $size; $i++) {
if ($arr[$i] > $N) {
return $i - 1;
}
}
return -1;
}
$arr = array(1, 2, 7, 8, 14, 19, 20, 24, 28);
$N = 17;
$index = find_the_floor($arr, $N);
if ($index == -1) {
echo "The floor doesn\'t exist in array";
}
else {
echo "The floor of " . $N . " is " . $arr[$index];
}
/*
run:
The floor of 17 is 14
*/