我希望我的数组按唯一日期排序,并且只显示一次日期值,包括当天的所有时间。
所需示例:
非正式的
星期一:下午 2 点至下午 3 点,下午 3 点至下午 4 点,下午 4 点至下午 5 点
星期二:下午 2 点至下午 3 点,下午 3 点至下午 4 点,下午 4 点至下午 5 点
星期三:下午 2 点至下午 3 点,下午 3 点至下午 4 点,下午 4 点至下午 5 点
星期四:下午 2 点至下午 3 点,下午 3 点至下午 4 点,下午 4 点至下午 5 点
目前,我每次都在迭代的那一天得到这个。周一:2PM 到 3PM,周一 3PM 到 4PM,周一 4PM 到 5PM 周二:2PM 到 3PM,周二 3PM 到 4PM,周二 4PM 到 5PM 等等。
这是我的数组:
["Informal"]=> array(12) {
[0]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(6) "Monday" ["start_time"]=> string(4) "2 PM" ["end_time"]=> string(4) "3 PM" } }
[1]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(6) "Monday" ["start_time"]=> string(4) "3 PM" ["end_time"]=> string(4) "4 PM" } }
[2]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(6) "Monday" ["start_time"]=> string(4) "4 PM" ["end_time"]=> string(4) "5 PM" } }
[3]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(7) "Tuesday" ["start_time"]=> string(4) "2 PM" ["end_time"]=> string(4) "3 PM" } }
[4]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(7) "Tuesday" ["start_time"]=> string(4) "3 PM" ["end_time"]=> string(4) "4 PM" } }
[5]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(7) "Tuesday" ["start_time"]=> string(4) "4 PM" ["end_time"]=> string(4) "5 PM" } }
[6]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(9) "Wednesday" ["start_time"]=> string(4) "2 PM" ["end_time"]=> string(4) "3 PM" } }
[7]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(9) "Wednesday" ["start_time"]=> string(4) "3 PM" ["end_time"]=> string(4) "4 PM" } }
[8]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(9) "Wednesday" ["start_time"]=> string(4) "4 PM" ["end_time"]=> string(4) "5 PM" } }
[9]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(8) "Thursday" ["start_time"]=> string(4) "2 PM" ["end_time"]=> string(4) "3 PM" } }
[10]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(8) "Thursday" ["start_time"]=> string(4) "3 PM" ["end_time"]=> string(4) "4 PM" } }
[11]=> array(1) { ["Schedule"]=> array(4) { ["program"]=> string(8) "Informal" ["day"]=> string(8) "Thursday" ["start_time"]=> string(4) "4 PM" ["end_time"]=> string(4) "5 PM" } } }
这个数组有可能还是我需要修改我的 sql 查询?我尝试了 GROUP BY 'day' 但它没有用。我还尝试在一天的 foreach 循环中嵌套时间的 foreach 循环,我认为这是要走的路,但我没有得到正确的结果。
这是没有嵌套的 php/html
<h1>Schedules</h1>
<h2>Room 1</h2>
<?php
$array = array();
foreach ($data as $row) {
$data[$row['Schedule']['program']][] = $row;
}
?>
<?php foreach ($array as $program => $schedules) { ?>
<h3><?php echo $program; ?></h3>
<fieldset class="programs">
<!-- data rows -->
<?php foreach ($schedules as $key => $schedule) :
echo $schedule['Schedule']['day']. " : " .$schedule['Schedule']['start_time'];?> to <?php echo $schedule['Schedule']['end_time'] . " <br /> ";
endforeach; ?>
</fieldset>