473,468 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Iterate from middle of array

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

Jul 17 '05 #1
6 3923
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 =--
Jul 17 '05 #2
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/
Jul 17 '05 #3

"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
Jul 17 '05 #4

"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
Jul 17 '05 #5
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

Jul 17 '05 #6
"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
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: deko | last post by:
I have a file that contains the output of time(). A different time is on each line of the file, and each line represents a visit to the website. I want to calculate the total visits per day,...
2
by: maciej | last post by:
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), -...
1
by: | last post by:
I'm going a little crazy trying to learn how to use arrays as properties in VBScript classes. Hopefully someone can help. First, I can't figure out whether it's possible to iterate through the...
5
by: | last post by:
Hello, I have an array like this: Array ( =Array ( =Array ( =7A585DC0
5
by: Martin Pöpping | last post by:
Hello, I want to iterate the second dimension of a 2-dim-array. Let´s say I have an array: double myArray and a given index: int i. Assume my index is given in want to iterate my array...
5
by: Manish Tomar | last post by:
Hi, I know that using for in loop to iterate over array elements is a bad idea but I have a situation where it is tempting me to use it. I have a two dimensional sparse array say "arr" which...
1
by: nagarwal | last post by:
hi frnds, I am facing a prblm in displaying the contents of an ArrayList which contains 'String' Objects using logic:iterate tag.. example: in the Action : String s=null; String s1=null;...
25
RMWChaos
by: RMWChaos | last post by:
Any JSON experts out there? I'd like to know if it is possible, and if so how, to iterate through a JSON property list so that each iteration selects the next value for each object. Here is an...
4
by: im12345 | last post by:
I have the following question: Im doing a sample application using dojo and json. I have 2 classes: 1. Book class package com.esolaria.dojoex; import org.json.JSONObject; import...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.