PHP: Next Available Value in an Array, starting with a non-indexed value
Posted
by Erik Smith
on Stack Overflow
See other posts from Stack Overflow
or by Erik Smith
Published on 2010-03-26T18:09:20Z
Indexed on
2010/03/26
18:13 UTC
Read the original article
Hit count: 303
I've been stumped on this PHP issue for about a day now. Basically, we have an array of hours formatted in 24-hour format, and an arbitrary value ($hour) (also a 24-hour). The problem is, we need to take $hour, and get the next available value in the array, starting with the value that immediately proceeds $hour.
The array might look something like: $goodHours = array('8,9,10,11,12,19,20,21).
Then the hour value might be: $hour = 14;
So, we need some way to know that 19 is the next best time. Additionally, we might also need to get the second, third, or fourth (etc) available value.
The issue seems to be that because 14 isn't a value in the array, there is not index to reference that would let us increment to the next value.
To make things simpler, I've taken $goodHours and repeated the values several times just so I don't have to deal with going back to the start (maybe not the best way to do it, but a quick fix).
I have a feeling this is something simple I'm missing, but I would be so appreciative if anyone could shed some light.
Erik
© Stack Overflow or respective owner