数组中最接近参考值的值

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;
}

Obviously, to make this as efficient as possible you're going to first want to sort the array by the timestamps. Then, you will need to write your own closest function like this one.

Since you say you don't want to use a loop, which is how you would normally do it, you'll have to implement some kind of hashing function for the array indices that keeps the array sorted by timestamp value. Then, you can insert the timestamp value if it doesn't exist and then go to the next array index.