- range: documentation ( source)
<?php
$data = range(1, 18);
// You can either index by 0 and add one, or 1 and subtract one
for($i = 1; $i <= count($data); $i++) {
// Variable for easier reference later
$item = $data[$i - 1];
// On the fifth item, emit logic for just one
if(0 === $i % 5){
echo 'Single: ' . $item;
}else {
// Grab the next if it exsts
$next = $data[$i++] ?? null;
if($next){
echo 'Double: ' . $item . ', ' . $next;
}else{
echo 'Double: ' . $item;
}
}
echo PHP_EOL;
}