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);

preferences:
23.12 ms | 405 KiB | 5 Q