Iterate from middle of array – PHP

  php

Q(Question):

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

A(Answer):

ChronoFish wrote (in looooooooooooooooooooooooooooooooong lines):

I want to iterate through an array starting at a known index.
However the indexes are not linear.
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)
# {
// also use the index inside your loop
foreach ($eventList as $k=>$currentEvent)
{

…. // processing
$startEvent = 10642635555;
$endEvent = 1064267782;

// move the $startEvent and $endEvent outside the loop,
// unless, of course, they are changing inside 🙂
# for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
# {

// use the $k thing
if ($startEvent <= $k && $k <= $endEvent) print $currentEvent;
# print $eventList[$event];
# } …. // more processing
}

This way you only go through the array once.
I really didn’t understand if you wanted multiple prints of each event
between $startEvent and $endEvent; if you do, this wil not work.

–= my mail box only accepts =–
–= Content-Type: text/plain =–
–= Size below 10001 bytes =–

A(Answer):

ChronoFish wrote:

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.

Here’s my first thought…

$tmp=$eventList; // ksort doesn’t make a copy of the passed array,
ksort($tmp); // it modifies it – need to copy if you want the
// original for use later

foreach($tmp as $k=>$event){
if($k<$startEvent) continue; // not at the start yet
else if($k>$endEvent) break; // last one has been done
else echo $event; // print it out
}
unset($tmp);


Justin Koivisto – sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
SEO Competition League: http://seo.koivi.com/

A(Answer):

"Pedro Graca" <he****@hotpop.com> wrote in message news:c22dpo$1p4bac$1@ID-
…..

This way you only go through the array once.
I really didn’t understand if you wanted multiple prints of each event
between $startEvent and $endEvent; if you do, this wil not work.

Thanks so much Pedro. I don’t think I stated my intentions strong enough. I do need to go through multiple times as startEvent and
endEvent change on each iteration. I endedup just biteing the bullet and iterate twice through.

I think what I was after was the opposite of the pos() function. It gives you the current key. What I want to do is to be able to
set the "current" position of the array position pointer.

Thanks anyway!

CF

A(Answer):

"Justin Koivisto" <sp**@koivi.com> wrote in message news:%H*****************@news7.onvoy.net…

$tmp=$eventList; // ksort doesn’t make a copy of the passed array,
ksort($tmp); // it modifies it – need to copy if you want the
// original for use later

foreach($tmp as $k=>$event){
if($k<$startEvent) continue; // not at the start yet
else if($k>$endEvent) break; // last one has been done
else echo $event; // print it out
}
unset($tmp);

Hi Justin, Thanks for your help. This is what I was trying to avoid as this gives me 0(n^2) (for every element I iterate through
ever element). As noted in my other response what I was looking for was the opposite of the pos() function. pos() returns the
current key of the arrays internal position pointer. I want to be able to to set it like pos($currentEvent) and continue iterating
from there. Oh well….

Thanks again,
CF

A(Answer):

Try this:

$indices = array_keys($eventList);
$start = array_search($indices, $startEvent);
$end = array_search($indices, $endEvent);
$range = array_slice($eventList, $start, $end – $start + 1);

or this

$positions = array_flip(array_keys($eventList));
$start = $positions[$startEvent];
$end = $positions[$endEvent];
$range = array_slice($eventList, $start, $end – $start + 1);

$range will be the elements you’re interested in.

Uzytkownik "ChronoFish" <de**@chronofish.com> napisal w wiadomosci
news:bs21c.8082$oP.2717@lakeread03…

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

A(Answer):

"Chung Leong" <ch***********@hotmail.com> wrote in message news:<DP********************@comcast.com>…

Try this:

$indices = array_keys($eventList);
$start = array_search($indices, $startEvent);
$end = array_search($indices, $endEvent);
$range = array_slice($eventList, $start, $end – $start + 1);

Great idea. I had not considered slice and I think this will work
fine. Not sure what it means in terms of "Big O" but it looks clean.

Thanks!
CF

LEAVE A COMMENT