- sort: documentation ( source)
- range: documentation ( source)
<?php
$arr = range(0,1000);
function closest($arr, $x) {
$result = "default";
sort($arr);
foreach($arr as $value) {
if($value < $x) {
$result = $value;
} else
break;
}
return $result;
}
echo closest($arr,17)."\n";
echo closest($arr,20)."\n";
echo closest($arr,6);