I have an array in codeigniter like below. As shown below, there are some spaces between dates. After 1st April, there is 3rd and after that its 10th and so on.
Array
(
[2019-04-01] => 1
[2019-04-03] => 3
[2019-04-10] => 8
[2019-04-15] => 1
)
I need those empty dates to be there with a value '0' so at last, the array will look like:
Array
(
[2019-04-01] => 1
[2019-04-02] => 0
[2019-04-03] => 3
[2019-04-04] => 0
[2019-04-05] => 0
[2019-04-06] => 0
[2019-04-07] => 0
[2019-04-08] => 0
[2019-04-09] => 0
[2019-04-10] => 8
[2019-04-11] => 0
[2019-04-12] => 0
[2019-04-13] => 0
[2019-04-14] => 0
[2019-04-15] => 1
)
The first and last index of array will work as range.
What I've tried so far
I was using this approach but this doesn't seem to work. In below code, testArr is the array posted above
$i=0;
foreach ($testArr as $key => $value) {
if($i==count($testArr)-1){break;}
$currentDay=new DateTime($key);
$nextDay=new DateTime(array_keys($testArr)[$i]);
$diff=date_diff($nextDay,$currentDay);
$diffint=intval($diff->format("%a"));
if($diffint!==1){
$currentDay->modify('+1 day');
$temp=array($currentDay->format('Y-m-d')=>0);
$testArr = $this->insertArrayAtPosition($testArr,$temp,$i);
}
$i++;
}
If this question already has answers somewhere, then don't forget to mark it as duplicate.