3👍
✅
Don’t use an associative array, use an indexed array. Start by creating an array with 53 elements, then update it in the loop.
$data = array_fill(0, 53, 0);
while($row = $stmt->fetch()){
$date = new DateTime($row['datetime_start']);
$week = $date->format("W");
$data[intval($week)]++;
}
0👍
Since JSON keeps the same array structure as the predecessor, you can sort the array into order prior to translating it to JSON.
ksort($data); // Add this before
json_encode($data); // This line
ksort()
returns a boolean, thus it must be declared as its own statement prior to using json_encode()
.
Source:stackexchange.com