Iterate from middle of array

From: ChronoFish (deja_at_chronofish.com)
Date: 03/02/04


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



Relevant Pages

  • Re: newbie: problem with mysql_fetch_array
    ... // this will iterate 4 times ... what I don't understand is I get an array count of 4, but if I look in the ... mysql_fetch_array only fetches one row from the result. ... instead of the foreach you had there, ...
    (comp.lang.php)
  • Re: Get an arbitrary hash key, quickly.
    ... and iterate through that snapshot. ... Except when the LIST of the foreach has an array in it, ...
    (comp.lang.perl.misc)
  • Re: How to iterate through array?
    ... "deko" wrote in message ... > file into an array, and then iterate through the array testing each ... You've used foreach() incorrectly. ...
    (comp.lang.php)
  • Re: How to iterate through array?
    ... > file into an array, and then iterate through the array testing each ... foreach ($avs as $val) ...
    (comp.lang.php)
  • Re: Iterate from middle of array
    ... > I want to iterate through an array starting at a known index. ... However the indexes are not linear. ... Please use comp.lang.php for PHP related questions, ...
    (comp.lang.php)