I have an array that contains time stamps with associated "left" or "right" values:
array (size=237)
1421439428 => string 'left' (length=4)
1421439411 => string 'right' (length=5)
1421439392 => string 'left' (length=4)
[here goes the example TS from below]
1421439380 => string 'right' (length=5)
1421439358 => string 'left' (length=4)
1421439329 => string 'right' (length=5)
1421439240 => string 'right' (length=5)
1421439234 => string 'left' (length=4)
Now I want to give a time stamp, e.g. 1421439391 (that is or is not in the keys) and I want to know, what is the most recent value. In this case "right". Even if it is closer to the left value I want to know the value below!
How is that possible (without a loop)?
With loop (based on function linked by Alex W):
function closest($array, $number) {
foreach ($array as $key=>$val) {
if ($key <= $number) return $val;
}
return end($array); // or return NULL;
}