'segunda','Ter'=>'terça','Qua'=>'quarta','Qui'=>'quinta','Sex'=>'sexta','Sab'=>'sábado']; $rows = $data ?? []; $week_values = array_values($week_day); $today_map = [1=>'segunda', 2=>'terça', 3=>'quarta', 4=>'quinta', 5=>'sexta', 6=>'sábado']; $default_day = $today_map[(int) date('N')] ?? null; if (!in_array($default_day, $week_values, true)) { $default_day = $week_values[0]; } // Distinct start times → grid rows; days → columns. Split Manhã (<12:00) / Tarde. $slot_times = []; foreach ($rows as $d) { $slot_times[$d['hora_inicio']] = true; } ksort($slot_times); $slot_times = array_keys($slot_times); $morning = $afternoon = []; foreach ($slot_times as $t) { if ((int) substr($t, 0, 2) < 12) { $morning[] = $t; } else { $afternoon[] = $t; } } $row_of = []; $sections = []; $gr = 2; // row 1 = day headers if ($morning) { $sections[] = ['Manhã', $gr++]; foreach ($morning as $t) { $row_of[$t] = $gr++; } } if ($afternoon) { $sections[] = ['Tarde', $gr++]; foreach ($afternoon as $t) { $row_of[$t] = $gr++; } } $total_rows = $gr - 1; $grid = []; foreach ($rows as $d) { $grid[$d['dia_semana']][$d['hora_inicio']][] = $d; } ?>