Get closest, lower and higher number as a associative array from a array
<?php
function closestLowerHigherNr($array, $nr) {
sort($array);
$re_arr = array('lower'=>min(current($array), $nr), 'higher'=>max(end($array), $nr), 'closest'=>$nr);
foreach($array AS $num){
if($nr > $num) $re_arr['lower'] = $num;
else if($nr <= $num){
$re_arr['higher'] = $num;
break;
}
}
$re_arr['closest'] = (abs($nr - $re_arr['lower']) < abs($re_arr['higher'] - $nr)) ? $re_arr['lower'] : $re_arr['higher'];
return $re_arr;
}
print_r(closestLowerHigherNr($array,180));
// The above function will return a associative array as
/*
Array (
[lower] => 150
[higher] => 200
[closest] => 200
)
*/