Hi there,
I want to iterate through an array starting at a known index. However the indexes are not linear.
For example I have an array of events keyed by timestamp.
$eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 =>
"event5");
I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).
I'm looking for something Like
$startEvent = 10642635555;
$endEvent = 1064267782;
for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
{
print $eventList[$event];
}
To make it worse, I can't use a foreach and simply "if" my way out of it because I am already iterating over the entire list and I
would like to avoid a O(x^2) algorithm.
foreach ($eventList as $currentEvent)
{
..... // processing
$startEvent = 10642635555;
$endEvent = 1064267782;
for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
{
print $eventList[$event];
}
..... // more processing
}
Of course there is no "nextKey()" and before rolling my own I thought I would ask if PHP already has a solution for this that I've
missed.
Thanks!
CF