I could not identify any pattern in the sequence that could facilitate its creation. The closest I came was doing:
foreach (range(0, 23) as $i)
echo ($i*8 + 1) % 23, ' ';
But the way out is:
1 9 17 2 10 18 3 11 19 4 12 20 5 13 21 6 14 22 7 15 0 8 16 1
Obviously failing when the output value should be 23 or 24, since the general term used will never return a value greater than 22, that is, it worked for 91.67% of the sequence.
However, taking advantage of its logic, if you need to iterate on these values, you should define a generator:
function sequencia() {
foreach (range(1, 8) as $i) {
yield $i;
yield $i + 8;
yield $i + 16;
}
}
This way you can take advantage of the returned values - instead of just printed on the screen:
foreach(sequencia() as $i)
echo $i, ' ';
Generating:
1 9 17 2 10 18 3 11 19 4 12 20 5 13 21 6 14 22 7 15 23 8 16 24
If you like a more functional footprint:
function sequencia()
{
return array_reduce(
array_map(
function ($x, $y, $z) {
return [$x, $y, $z];
},
...array_chunk(range(1, 24), 8)
),
"array_merge",
[]
);
}
The result will be exactly the desired sequence, but in my view, is to add complexity without reasons. As Maniero said, elegance is relative.