Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
// always make such numbers configurable
$num_lookaheads = 5;
// test array
$images = [1,2,3,4];
// limit the number of lookaheads if array is too small
$limit = min($num_lookaheads, count($images));
foreach ($images as $position => $value)
{
// current item
echo $value,"\n";
// getting look-aheads
// taking the missing items from the beginning
$beginning = 0;
for($i = 1; $i < $limit; $i++) {
$ahead = $images[$position + $i] ?? $images[$beginning++];
echo " ", $ahead,"\n";
}
}