3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [-5, 1, 2, 3, 10, 11, 12, 13, 14, 23, 24, 25, 31]; for ($i = 1, $count = count($array); $i < $count; ++$i) { // deliberately re-count the array because array_splice() extends the array if (($array[$i - 1] + 1) != $array[$i]) { // there is a gap between consecutive comparable values array_splice($array, $i++, 0, 'xxx'); // inject new element in gap, then bump counter to avoid injected element ++$count; } } var_export($array);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
array ( 0 => -5, 1 => 'xxx', 2 => 1, 3 => 2, 4 => 3, 5 => 'xxx', 6 => 10, 7 => 11, 8 => 12, 9 => 13, 10 => 14, 11 => 'xxx', 12 => 23, 13 => 24, 14 => 25, 15 => 'xxx', 16 => 31, )

preferences:
70.84 ms | 407 KiB | 5 Q