Iterate from middle of array
From: ChronoFish (deja_at_chronofish.com)
Date: 03/02/04
- Next message: Josh: "Re: help!!!!!!!! MySQL, PHP, and Apache configuration."
- Previous message: Shawn Wilson: "Re: Need help with gallery script I am writing"
- Next in thread: Pedro Graca: "Re: Iterate from middle of array"
- Reply: Pedro Graca: "Re: Iterate from middle of array"
- Reply: Justin Koivisto: "Re: Iterate from middle of array"
- Reply: Chung Leong: "Re: Iterate from middle of array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 2 Mar 2004 11:17:06 -0500
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
- Next message: Josh: "Re: help!!!!!!!! MySQL, PHP, and Apache configuration."
- Previous message: Shawn Wilson: "Re: Need help with gallery script I am writing"
- Next in thread: Pedro Graca: "Re: Iterate from middle of array"
- Reply: Pedro Graca: "Re: Iterate from middle of array"
- Reply: Justin Koivisto: "Re: Iterate from middle of array"
- Reply: Chung Leong: "Re: Iterate from middle of array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|