Jump to content

struggling with last entry in array in for loop


a.mlw.walker

Recommended Posts

So I have a list of events happening on certain dates

 

Date

event

event

event

Date

event

event

Date

event

Date

event

event

i.e the number of events per date change. The whole list is stored in an array called $matches. Depending on the formatting of the event or date tells the computer (or me) whether it is an event or a date. So the locations of the dates within $matches[] are stored in the array $dayKeys, and the position of the events within $matches[] is stored in the array $tournKeys.

I am trying to print the list back out in the correct order.

foreach($dayKeys as $a => $b)
{
$c = $a;
foreach($tournKeys as $c => $d)
	{
if (($tournKeys[$c] > $dayKeys[$a]) && ($tournKeys[$c] < $dayKeys[$a+1])) //if both are true print the events until it is required to print the next date as oppposed to an event.
			{
......print the data from matches.....
}
}
}

I have tried to simplify the above loops as much as possible for this thread. My point is though, that when I am at the end of $dayKeys,  $dayKeys[$a+1] generates an error, because there are no more $dayKeys.

So my question is how should I rephrase that if condition so that I dont have to look to see whether The next thing to print is a date or an event by looking at the next $dayKeys value?

Link to comment
Share on other sites

Have I foxxed you all? I cant think of a solution either!

I have seen that the function next(); exists. Perhaps I should be using this in some way. Im thinking if (next($dayKeys) exists)??? or something that was a bit pseudo but you know what I mean?

 

What about using http://www.php.net/manual/en/function.array-key-exists.php in some way? its confusing me, however I can see there must be a way to get around it...

Link to comment
Share on other sites

OK So i managed to get it to not produce the error by changing the condition to:

if ((count($tournKeys) == $c+1) || (($gameKeys[$e] > $tournKeys[$c]) && ($gameKeys[$e] < $tournKeys[$c+1])))

however if my list were:

Date1

event1

event2

event3

Date2

event4

event5

Date3

event6

Date4

event7

event8

It now outputs this:

Date1

event1

event2

event3

Date2

event4

event5

Date3

event6

Date4

event7

event8

event1

event2

event3

event4

event5

event6

event7

event8

i.e it seems to print again. Any ideas why changing the condition like that would cause that to happen?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.