calmchess Posted March 16, 2009 Share Posted March 16, 2009 I need to run a for loop on an array and read ahead 1 index so that if that spot is null then i can break out of the for loop something like this but it doesn't work..... $whopro = array("slot0","slot1","slot2"); for($l=17; $l < 34; $l++){ if($whopro[$i+1]==null){ break; } echo $whopro[i]; } now obviously the loop loops mor times than there are array indexes.The whole point of this is to stop getting php notice errors without changing the for loop integers. Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/ Share on other sites More sharing options...
Mark Baker Posted March 16, 2009 Share Posted March 16, 2009 Why can't you change the array integers.... especially as they're the whole cause of your problem Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786250 Share on other sites More sharing options...
calmchess Posted March 16, 2009 Author Share Posted March 16, 2009 I can't change the array integers because they are part of a dynamic array that changes length Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786252 Share on other sites More sharing options...
calmchess Posted March 16, 2009 Author Share Posted March 16, 2009 Bear with me and assume that there are above 17 items in the array Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786253 Share on other sites More sharing options...
laffin Posted March 16, 2009 Share Posted March 16, 2009 what is $i? why is $l way out of the array bounds $whopro has three elements, starting with 0, 1, 2 so trying to reference $l=17 gives ya an notice, about a variable not being declared. php dusn assign non existant variables to null. if the variable dusn exist why not just use isset? Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786255 Share on other sites More sharing options...
calmchess Posted March 16, 2009 Author Share Posted March 16, 2009 I know my code is bad above just fix it up and get on with the "look ahead" and break part Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786256 Share on other sites More sharing options...
laffin Posted March 16, 2009 Share Posted March 16, 2009 ya can perform a check within the for loop for($i=0;$i<=20;$i++) $slot[$i]="Slot{$i}"; for($l=17;$i<34 && issset($slot[$i]);$i++) echo $slot[$l]; but if u really need it within the loop, the lookahead (note that by program flow, the last valid entry wont display, as the lookahead breaks out of the loop) [/code] for($l=17;$i<34;$i++) { if(i!sset($slot[$i+1])) break; echo $slot[$l]; } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786264 Share on other sites More sharing options...
calmchess Posted March 16, 2009 Author Share Posted March 16, 2009 Thankyou so much that worked perfectly and pardon my dirty post. Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786272 Share on other sites More sharing options...
laffin Posted March 16, 2009 Share Posted March 16, 2009 No Probs here There are a lot of php functions, some that even surprise me Good luck on yer project Quote Link to comment https://forums.phpfreaks.com/topic/149723-array-check-ahead-loop/#findComment-786279 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.