<?php
$rows = [
[
'time' => '00:00:00',
'sales' => 55.99,
'orders' => 1,
],
[
'time' => '06:00:00',
'sales' => 46.37,
'orders' => 1,
],
[
'time' => '08:00:00',
'sales' => 246.56,
'orders' => 4,
],
[
'time' => '10:00:00',
'sales' => 78.66,
'orders' => 1,
]
];
// isolate the times
$times = array_map(function ($row) {
return $row['time'];
}, $rows);
// create a list of similarly formatted times from 00:00:00 - 22:00:00
$allTimes = array_map(function ($hour) {
return sprintf('%02d:00:00', $hour);
}, range(0, 22, 2));
// compute the difference
$notTimes = array_diff($allTimes, $times);
// add the notTimes to the original rowset with default values
foreach ($notTimes as $notTime)
{
$rows[] = [
'time' => $notTime,
'sales' => 0,
'orders' => 0,
];
}
// sort, because why not
usort($rows, function ($rowOne, $rowTwo) {
return ((int) $rowOne['time']) > ((int) $rowTwo['time']);
});
// dump
var_dump($rows);
preferences:
43.09 ms | 402 KiB | 5 Q