php - Closest value of an array below a reference value -


I have an array that includes time stamps with related "left" or "true" values:

  array (size = 237) 1421439428 = & gt; String 'left' (length = 4) 1421439411 = & gt; String 'right' (length = 5) 1421439392 = & gt; String 'left' (length = 4) [Here is an example of TS] 1421439380 = & gt; String 'right' (length = 5) 1421439358 = & gt; String 'left' (length = 4) 1421439329 = & gt; String 'right' (length = 5) 1421439240 = & gt; String 'right' (length = 5) 1421439234 = & gt; The string 'left' (length = 4)  

Now I want to give a one time ticket, eg. 1421439391 (whether it's in the keys or not) and I want to know, what is the most recent value "right" in this case even if it is close to the left value then I want to know the value given below !

How is this possible (without any loop)?

with loop (Alex W):

  function closest ($ array, $ number) {foreach ($ key = $ val = & gt; $ val) {Return ($ key & lt; = $ number) return $ val; } End of return ($ array); // or return tap; }  

Obviously, to make it as efficient as possible you want to first After the time stamp to sort the array, you have to type your own the closest function.

Since you say that you do not want to use any loop, which usually you do this, then you have to implement any type of hashing function for the array index, which is the timestamp value Holds the sorted array. After that, you can insert timestamp values ​​if it does not exist and then go to the next array index.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -