- end: documentation ( source)
- array_filter: documentation ( source)
- sort: documentation ( source)
- range: documentation ( source)
<?php
$arr = range(0,1000);
function find_closest($arr,$x){
sort($arr);
$filtered_array = array_filter($arr,function($v)use($x){
return $v < $x;
});
return count($filtered_array) > 0 ? end($filtered_array) : "Your Message";
}
echo find_closest($arr,17)."\n";
echo find_closest($arr,20)."\n";
echo find_closest($arr,6);