【问题标题】:show results of array sorted by unique day显示按唯一日期排序的数组结果
【发布时间】:2013-07-29 17:05:57
【问题描述】:

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

所需示例:

非正式

星期一:下午 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 点 周二: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>

【问题讨论】:

  • 你的sql查询是什么?根据您的表结构,您可以使用 GROUP_CONCAT 将每天的预定时间分组到一个逗号分隔的列表中,然后您可以在 php 代码中使用 explode

标签: php multidimensional-array


【解决方案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>

【讨论】:

  • 这看起来也不错,但是在返回所需结果后,我用 [$key +1] 得到了两条线的未定义偏移量。我需要取消设置吗?
【解决方案2】:
<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 建议的那样。

【讨论】:

  • 这看起来不错,但有没有办法让每个新的一天都放在下一行?
【解决方案3】:

将 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")
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    相关资源
    最近更新 更多