t_machine Posted August 7, 2007 Share Posted August 7, 2007 I have an array that stores ids, each of which is unique. Is there a way to run a for loop and get two keys at a time instead of only one? Example $id = array(1, 2, 3, 4, 5, 6); for loop.... echo key 1 echo key 2 next loop echo key 3 echo key 4 and so on.... Each key must not appear twice so once a key is displayed, it will be removed from any further loops. I need it to display two keys at a time because I will be pairing them up for the next part of the script. Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/ Share on other sites More sharing options...
hitman6003 Posted August 7, 2007 Share Posted August 7, 2007 for ($i = 0; $i < count($array); $i + 2) { echo $array[$i] . '<br />' . $array[$i + 1]; } Probably want to put in a check to see if $array[$i + 1] exists...there is a potential for it not to. Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317129 Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 maybe $x=1; for ($i = 0; $i < $x; $i) { foreach($array as $y){ // do something $c++; if($c==2) break; } if (count($array)!=$c) $x+=1; } not not tested and maybe thers better Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317135 Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 foreach($id as $value){ echo $value . " - " .next($id) . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317138 Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 i guess you have to explain or confirm the answer because your getting diff answer Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317139 Share on other sites More sharing options...
t_machine Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks everyone GuiltyGear is close to what i need but the result repeats each keys 1 - 2 2 - 3 3 - 4 4 - 5 The perfect scenario would be the following 1 - 2 3 - 4 5 - 6 7 - 8 I tried the other two examples but my comp nearly crashed from the loops that kept continuing Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317176 Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 Thought u needed it that way. In this case the code provided by hitman6003 is your case. Probably they crashed cos in the first it is '$i + 2' and not '$i += 2' and in the second the $i is not incremented. Anyway this code should do it: for($i=0; $i<count($id); $i += 2){ echo $id[$i] . ' - ' . $id[$i+1] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317186 Share on other sites More sharing options...
hitman6003 Posted August 7, 2007 Share Posted August 7, 2007 '$i + 2' and not '$i += 2' and in the second the $i is not incremented. DOH!! Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317188 Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 lol. What i was saying is that u cant use $i+2 to increment $i by 2. U must use $i=$i+2 or simply $i += 2. At least at my side it is entering an endless loop. The other thing is that teng84 wrote a for loop without incrementing $i. But guess these arent important in an example code. Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317192 Share on other sites More sharing options...
t_machine Posted August 7, 2007 Author Share Posted August 7, 2007 Thanks very much. It works perfectly now Quote Link to comment https://forums.phpfreaks.com/topic/63642-solved-how-to-get-two-keys-from-array-loop/#findComment-317195 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.