Jump to content

[SOLVED] Stagered Looping?


sw41

Recommended Posts

If there an easy way to make a loop that makes this sequence of numbers where I define $x and then it sequences like below?

 

1 2 3 4 5 6 7 8 $x    $x 8 7 6 5 4 3 2 1

2 3 4 5 6 7 8 $x 1    1 $x 8 7 6 5 4 3 2

3 4 5 6 7 8 $x 1 2    2 1 $x 8 7 6 5 4 3

4 5 6 7 8 $x 1 2 3    3 2 1 $x 8 7 6 5 4

... and so on until all numbers are used

$x 1 2 3 4 5 6 7 8    8 7 6 5 4 3 2 1 $x

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/160852-solved-stagered-looping/
Share on other sites

not sure why, but i ignored taht for some reason

 

<?php
$x = isset($_GET['x']) ? $_GET['x'] : 8;
for($i=1; $i<=$x; $i++) {
for($j=$i; $j<$x+$i; $j++) {
	echo $j > $x ? $j-$x." " : "$j ";
}
$j--;
for($j; $j>=0+$i; $j--) {
	echo $j > $x ? $j-$x." " : "$j ";
}
echo '<br />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.