a.mlw.walker Posted January 22, 2012 Share Posted January 22, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255523-struggling-with-last-entry-in-array-in-for-loop/ Share on other sites More sharing options...
a.mlw.walker Posted January 23, 2012 Author Share Posted January 23, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/255523-struggling-with-last-entry-in-array-in-for-loop/#findComment-1310156 Share on other sites More sharing options...
a.mlw.walker Posted January 23, 2012 Author Share Posted January 23, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/255523-struggling-with-last-entry-in-array-in-for-loop/#findComment-1310344 Share on other sites More sharing options...
beegro Posted January 23, 2012 Share Posted January 23, 2012 Let me know if I'm missing something but why wouldn't you just use a 2-dimensional array like $elem['Date1'] = array('event1', 'event2', 'event3'); Quote Link to comment https://forums.phpfreaks.com/topic/255523-struggling-with-last-entry-in-array-in-for-loop/#findComment-1310362 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.