【问题标题】:Print Table of results for every day of the year打印一年中每一天的结果表
【发布时间】:2011-06-22 22:07:46
【问题描述】:

我有一个看起来像这样的数组。

Array (
    [1] => Array (
            [1] => Array ([0] => 12)
            [2] => Array ([0] => 23)
             # up to 12.
     # up to 31.

它从数组 1 到 31(每个月的每一天)运行,然后在数组 1-12(每个月)内运行。

我需要创建一个 HTML 表格,如下所示:

Date          January February and so on... to December
2011-01-01    12      23

所以,第一列和第一行的日期是月份。

创建此表的最佳方法是什么? 非常感谢您的帮助。

【问题讨论】:

  • 请向我们展示您已经尝试过的内容。

标签: php html date loops


【解决方案1】:

最好的方法是将数组转换为包含所有日期(行)的新数组,并且每行包含另一个包含所有月份(列)的数组。

这种格式更容易显示。

示例:

Array (
    [1] => Array (
            [1] => Array ([0] => 12)
            [2] => Array ([0] => 23)
             # up to 12.
     # up to 31.

Ups,无需转换!让我们给这个数组起个名字:$dates:

echo '<pre>';
foreach($dates as $rowIndex => $row) {
  printf('Row header (%2d):', $rowIndex);
  foreach($row as $col) {
      printf('%-10s', $col);
  }
  printf("<br>\n")
}
echo '</pre>';

我把顶部的标题留给你。

【讨论】:

    【解决方案2】:

    假设你的数组被称为$data

    <table>
      <thead>
        <th>Date</th>
        <th>January</th>
        <th>February</th>
        <th>March</th>
        <th>April</th>
        <th>May</th>
        <th>June</th>
        <th>July</th>
        <th>August</th>
        <th>September</th>
        <th>October</th>
        <th>November</th>
        <th>December</th>
      </thead>
      <tbody>
        <?php foreach ( $data as $key => $value ): ?>
          <tr>
            <th><?php echo date('Y-m-d', mktime(0, 0, 0, (int)date('m'), (int)$key)); ?></th>
            <?php for ( $i = 1; $i <= 12; $i++ ): ?>
              <td><?php echo ! empty($value[$i][0]) ? $value[$i][0] : '&nbsp;'; ?></td>
            <?php endfor; ?>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      相关资源
      最近更新 更多