I need to check an array for its keys. If they are consecutive, its good,if not i need to fill in some data.
e.g.
Array
(
[0] => 0
[1] => 1
[2] => 8
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[10] => 0
[11] => 0
[12] => 0
[14] => 0
[15] => 0
)
In this case, the indexes 9 and 13 are missing. To make the example easier, I just want to fill the missing data with the number 999.
My solution however is a little sloppy and doesn't work properly:
$oldK = 0;
foreach($array as $k=>$entry)
{
if($oldK !== $k)
{
$array[$oldK] = 999;
}
$oldK ++;
}
produces following output:
Array
(
[0] => 0
[1] => 1
[2] => 8
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 999
[10] => 999
[11] => 999
[12] => 999
[13] => 999
[14] => 0
[15] => 0
)
is there a smooth way that works?