3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $levels = [4=>40]; $a = array_keys($levels); sort($a); $max = end($a); $toAdd = array_diff(array_keys(array_fill(1, $max, 0)), $a); $result = array_fill_keys($toAdd, 0); $merged = $levels + $result; ksort($merged); var_dump($merged);
based on oK4T7
Output for 5.6.0 - 5.6.30, hhvm-3.18.5 - 3.22.0, 7.0.0 - 7.3.0beta1
array(4) { [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(40) }