doumi1852 2012-12-21 07:21
浏览 35
已采纳

如何使用PHP将日期数组转换为3级嵌套列表

I have an array of dates in descending chronilogical order. As I loop through them, currently I am splitting up the string and using and comparing the current year & month to previous.

Here is the desired output (example):

<ul>
    <li><h1>2012</h1>
<ul>
<li><h2>Jan</h2>
    <ul>
    <li>Jan 1, 2012</li>
    <li>Jan 5, 2012</li>
    </ul>
</li>
<li><h2>Feb</h2>
    <ul>
    <li>Feb 3, 2012</li>
    <li>Feb 8, 2012</li>
    </ul>
</li>
</ul>
    </li>
    <li><h1>2011</h1>
<ul>
<li><h2>Jan</h2>
    <ul>
    <li>Jan 2, 2011</li>
    <li>Mar 2, 2011</li>
    </ul>
</li>
<li><h2>Feb</h2>
    <ul>
    <li>Feb 9, 2011</li>
    <li>Feb 20, 2011</li>
    </ul>
</li>
</ul>
    </li>
    </ul>

This is one of my attempts:

$archiveDateHTML = '<ul id="menu">';
$c = 0;

foreach ($ArchiveDates as $k=>$value){
$dateArray = split(' ', $value);

if($currentYear !== $dateArray[2] && $c+1 < count($ArchiveDates)){
    if($c > 0){ $archiveDateHTML .= '</ul>';}
    $archiveDateHTML .= '<h2 class="yearTitle">'.$dateArray[2].'</h2>';
    $archiveDateHTML .= '<ul class="yearList">';
    $newYear = 1;
}
if($currentMonth !== $dateArray[0] && $c+1 < count($ArchiveDates)){
    if($c > 0 && $newYear==0){ $archiveDateHTML .= '</ul>';}
    $archiveDateHTML .= '<h3 class="monthTitle">'.$dateArray[0].'</h3>';
    $archiveDateHTML .= '<ul class="monthList">';
    $newYear = 0;
}

$archiveDateHTML .= $value;

$currentYear = $dateArray[2];
$currentMonth = $dateArray[0];
$c++;
}
$archiveDateHTML .= '</ul>';

There is some obvious problems, with the above code. I keep getting so close, but can't quite wrap my head around the logic. Please assist and thank you so much!

  • 写回答

2条回答 默认 最新

  • donoworuq450547191 2012-12-21 07:42
    关注

    Similar to paulgrav, this is my answer:

    $dates = array(
      'Jan 1, 2012',
      'Jan 5, 2012',
      'Feb 3, 2012',
      'Feb 8, 2012',
      'Jan 2, 2011',
      'Mar 2, 2011',
      'Feb 9, 2011',
      'Feb 20, 2011');
    
    $out = array();
    
    foreach ($dates as $date) {
      list($m, $d, $y) = explode(" ", $date);
      $out[$y][$m][] = $date;
    }
    
    foreach ($out as $year => $group) {
      echo "<ul>
    ";
      echo "  <li><h1>$year</h1>
    ";
      echo "    <ul>
    ";
      foreach ($group as $month => $dates) {
        echo "    <li><h2>$month</h2>
    ";
        echo "      <ul>
    ";
        foreach ($dates as $date) {
          echo "        <li>$date</li>
    ";
        }
        echo "      </ul>
    ";
        echo "    </li>
    ";
      }
      echo "    </ul>
    ";
      echo "  </li>
    ";
      echo "</ul>
    ";
    }
    

    OUTPUT

    <ul>
      <li><h1>2012</h1>
        <ul>
        <li><h2>Jan</h2>
          <ul>
            <li>Jan 1, 2012</li>
            <li>Jan 5, 2012</li>
          </ul>
        </li>
        <li><h2>Feb</h2>
          <ul>
            <li>Feb 3, 2012</li>
            <li>Feb 8, 2012</li>
          </ul>
        </li>
        </ul>
      </li>
    </ul>
    <ul>
      <li><h1>2011</h1>
        <ul>
        <li><h2>Jan</h2>
          <ul>
            <li>Jan 2, 2011</li>
          </ul>
        </li>
        <li><h2>Mar</h2>
          <ul>
            <li>Mar 2, 2011</li>
          </ul>
        </li>
        <li><h2>Feb</h2>
          <ul>
            <li>Feb 9, 2011</li>
            <li>Feb 20, 2011</li>
          </ul>
        </li>
        </ul>
      </li>
    </ul>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算