Jump to content

Recommended Posts

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 :)

 

 

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

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 />';
}

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.

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.