3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = [0, 1, 2, 3, 4, 5, 6, 7]; $count = count($a); $span = 5; // most sensible with odd numbers $center = (int)($span / 2); foreach ($a as $i => $v) { printf( "%d: %s\n", $i, implode( ',', array_slice( $a, min($count - $span, max(0, $i - $center)), $span ) ) ); }
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
0: 0,1,2,3,4 1: 0,1,2,3,4 2: 0,1,2,3,4 3: 1,2,3,4,5 4: 2,3,4,5,6 5: 3,4,5,6,7 6: 3,4,5,6,7 7: 3,4,5,6,7

preferences:
125.72 ms | 407 KiB | 5 Q