array_fill() cannot be used to setup only missing keys in an array. This
may be necessary for example before using implode() on a sparse filled
array.
The solution is to use this
function:
<?php
function array_setkeys(&$array, $fill =
NULL) {
牋$indexmax = -1;
牋for (end($array); $key = key($array);
prev($array)) {
牋牋if ($key > $indexmax)
牋牋牋$indexmax =
$key;
牋}
牋for ($i = 0; $i <= $indexmax; $i++) {
牋牋if
(!isset($array[$i]))
牋牋牋$array[$i] =
$fill;
牋}
牋ksort($array);
}
?>
This is
usefull in some situations where you don't know which key index was filled
and you want to preserve the association between a positioned field in an
imploded array and the key index when exploding it.