I have a PHP array like this..
Array
(
[04] => 1
[09] => 1
[10] => 1
[01] => 1
)
I want to make sure that it has keys 1-10 in it, if they do not exist then I want them to be added with a value of 0, so my final array looks like this..
Array
(
[01] => 1
[02] => 0
[03] => 0
[04] => 1
[05] => 0
[06] => 0
[07] => 0
[08] => 0
[09] => 1
[10] => 1
)
I am doing it like this..
if (!array_key_exists("01",$myarray)) {
$myarray['01'] = 0;
}
if (!array_key_exists("02",$myarray)) {
$myarray['02'] = 0;
}
if (!array_key_exists("02",$myarray)) {
$myarray['02'] = 0;
}
if (!array_key_exists("03",$myarray)) {
$myarray['03'] = 0;
}
And so on up until 10, then I do a ksort to get them in the right order
ksort($myarray)
This works but I am aware this is quite clunky and probably not very efficient.
Can anyone point me towards a better solution?