I have an array of rows with date values, but not all dates are guaranteed between the first and last row. I need to add rows so that every date has a row (a default value should be applied for the secondary column value).
Sample input:
$array = [
['date' => '2017-09-01', 'total' => 4],
['date' => '2017-09-03', 'total' => 6],
['date' => '2017-09-04', 'total' => 1],
['date' => '2017-09-05', 'total' => 3],
['date' => '2017-09-09', 'total' => 5]
];
I want to fill the date even if my query has no records on that date! How can I add the missing date index to the array. The dates are to be sequential/contiguous.
Desired output:
[
['date' => '2017-09-01', 'total' => 4],
['date' => '2017-09-02', 'total' => 0],
['date' => '2017-09-03', 'total' => 6],
['date' => '2017-09-04', 'total' => 1],
['date' => '2017-09-05', 'total' => 3],
['date' => '2017-09-06', 'total' => 0],
['date' => '2017-09-07', 'total' => 0],
['date' => '2017-09-08', 'total' => 0],
['date' => '2017-09-09', 'total' => 5]
]