I have an array which looks like this:
array:6 [▼
0 => 2
1 => 2
2 => 2
6 => 5
10 => 3
11 => 1
]
I would like to check for a range of numbers e.g. 0 to 11 if these keys exist in my array. If not I want to create the element with the missing key and give it the value 0.
So I would end up with an array like this:
array:6 [▼
0 => 2
1 => 2
2 => 2
3 => 0
4 => 0
5 => 0
6 => 5
7 => 0
8 => 0
9 => 0
10 => 3
11 => 1
]
I tried something like this:
$range = range(0,11);
foreach($myArray as $key => $value){
if(!in_array($key, $range)){
$myArray[$key] = 0;
}
}
But I just get the same array as at the beginning of the question.
array keysto withvalue 0??