0

我希望我的数组按唯一日期排序,并且只显示一次日期值,包括当天的所有时间。

所需示例:

非正式的

星期一:下午 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>
4

3 回答 3

1
<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">
<p>
<!-- data rows -->
<?php $current = ''; ?>
<php $counter = 0; ?>
<?php foreach ($schedules as $key => $schedule) :
echo $current != $schedule['Schedule']['day'] ? '</p><p>'.$schedule['Schedule']['day']. " : " : '';
echo ($counter!=0 ? ', ' : '') . $schedule['Schedule']['start_time'];?> to <?php echo $schedule['Schedule']['end_time'];

$current = $schedule['Schedule']['day'];
$counter++;
endforeach; ?>
</p>
</fieldset>

注意:代码未经测试。

编辑:代码更新。这不是最好的方法,但它确实有效。更好的选择可能是创建查询,该查询将获得所有日期和连续时间,如下面的@patrik 建议。

于 2013-07-29T17:12:36.087 回答
1

它会是这样的:

<?php foreach ($array as $program => $schedules) { ?>
<h3><?php echo $program; ?></h3>
<fieldset class="programs">
<!-- data rows -->
<?php 
$i = 0;
foreach ($schedules as $key => $schedule) :

    if ($i === 0) {
       echo $schedule['Schedule']['day'] . ': ';
       ++$i;
    }
echo $schedule['Schedule']['start_time'];?> to <?php echo $schedule['Schedule']['end_time'];
if (isset($schedules[$key +1 ]['Schedule']['day'])) {
if ($schedule['Schedule']['day'] === $schedules[$key +1 ]['Schedule']['day']) {
echo ', ';
} else {
    echo '</br> ' .$schedules[$key +1 ]['Schedule']['day'] . ': ';
}
}
endforeach; ?>
</fieldset>
于 2013-07-29T17:24:49.163 回答
0

将 CONCAT 与 GROUP_CONCAT 一起使用

SELECT
 day
 GROUP_CONCAT( CONCAT(start_time, " to ", end_time) ) as hours
FROM
 sometable
GROUP BY
 day

您从 mysql 查询返回的行将具有如下结构

array( 
   array("day"=>"Monday","hours"=>"2pm to 3pm, 3pm to 4pm, 4pm to 5pm"),
   array("day"=>"Tuesday","hours"=>"2pm to 3pm, 3pm to 4pm, 4pm to 5pm")
)
于 2013-07-29T17:27:07.017 回答