我希望我的数组按唯一日排序,只显示当天的值,包括当天的所有时间。
期望的例子:
非正式
星期一:下午2点至下午3点,下午3点至下午4点,下午4点至下午5点
星期二:下午2点至下午3点,下午3点至下午4点,下午4点至下午5点
周三:下午2点至下午3点,下午3点至下午4点,下午4点至下午5点
星期四:下午2点至下午3点,下午3点至下午4点,下午4点至下午5点
目前我每次迭代都会得到这个。 周一:下午2点至下午3点,周一下午3点至下午4点,周一下午4点至下午5点 周二:下午2点至下午3点,周二下午3点至下午4点,周二下午4点至下午5点 等等。
这是我的数组:
["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'日'但它没有用。我也尝试在当天的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>
答案 0 :(得分: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建议的所有日期和结束时间。
答案 1 :(得分: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>
答案 2 :(得分: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")
)