douyi1899 2013-11-13 21:28
浏览 58
已采纳

php单维数组

I'm trying to output an array containing events which is ical file output to be precise. Array in itself looks as below:

  Array (
    [0] => Array (
        [UID] => 4C2DDB6E-5B5B-4B97-BEBE-FF424BB154F8
        [DTEND] => 20131116 [SUMMARY] => test1_ONS
        [LAST-MODIFIED] => 20131111T210530Z
        [DTSTAMP] => 20131111T213437Z
        [DTSTART] => 20131112
        [LOCATION] => City
        [SEQUENCE] => 1
        [DESCRIPTION] => description here_http://www.test.com/test1
        [SUMMARY] => some text here
) )

I'm able to loop trough its records and output all events using the following foreach loop:

    foreach ($events_sorted as $event) {
$client = current(explode("_", $event['SUMMARY']));
} and so on...

The tricky part is that my table layout for events is based on months and looks as follows:

             CLIENT   EXHIBITION   CITY
_________________________________________
OCT 2013     client   exhibition   city
             client   exhibition   city
             client   exhibition   city
________________________________________
NOV 2013     client   exhibition   city
             client   exhibition   city
________________________________________
DEC 2013     client   exhibition   city

What I'm unable to do is to loop trough all the events, output their corresponding month name once as per layout, output their data for the corresponding events and proceed to the next month.

My table layout however is built the way that its its first row contains month name data and has to be somehow distinguished from all other rows containing events:

<table id="table_events">
                <tbody><tr id="header_row">
                <th id="date_header"></th>
                <th></th>
                <th></th>
                <th></th>
                </tr>

                <tr class="month_start">
                <td>
                <div class="month_rect">
                    <div class="month_rect_text month_long">SEPT</div>
                    <div class="month_rect_year">2013</div>
                </div>
                </td>
                <td>eee</td>
                <td>eee</td>
                <td>Oslo</td>
                </tr>

                <tr>
                <td>
                    </td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>

                <tr>
                    <td></td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>
                <tr id="oct" class="month_start">
                <td>
                <div class="month_rect">
                    <div class="month_rect_text month_short">OCT</div>
                    <div class="month_rect_year">2013</div>
                </div>
                </td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>
                <tr>
                <td>
                    </td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>
                <tr class="month_start">
                <td>
                <div class="month_rect">
                    <div class="month_rect_text month_short">NOV</div>
                    <div class="month_rect_year">2013</div>
                </div>
                </td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>

                <tr>
                <td>
                    </td>
                <td>test</td>
                <td>test</td>
                <td>test</td>
                </tr>

                </tbody></table>

Until now I also tried creating new array based on months but with no luck either:

 foreach($events_sorted as $test) {
$month_number = gmdate("m", $ical->iCalDateToUnixTimestamp($test['DTSTART']));

$r = array("month" => $month_number);

}

My last idea was to print out all events and by resetting array check for existence of month name, if it exists then do not output it again and use different table row layout but I'm unsure how to implement this solution. I hope there is easier way to achieve described effect

  • 写回答

1条回答 默认 最新

  • duagfgfn1981 2013-11-13 22:40
    关注
    <table>
        <tr>
          <td width="20%"></td>
          <td>CLIENT</td>
          <td>EXHIBITION</td>
          <td>CITY</td>
        </tr>
        <?php $last_event = NULL; ?>
        <?php foreach($events as $event) : ?>
        <tr>
          <td><?php if($last_event != NULL) : if($last_event['DATE'] != $event['DATE']) : echo $event['DATE']; endif; else : echo $event['DATE']; endif; ?></td>
          <td><?php echo $event['CLIENT']; ?></td>
          <td><?php echo $event['EXHIBITION']; ?></td>
          <td><?php echo $event['CITY']; ?></td>
        </tr>
        <?php $last_event = $event; ?>
        <?php endforeach; ?>
      </table>
    

    There is a $last_event variable which is going to hold every event that has passed through foreach. There is an if statement in loop which determines that if the $last_event has the same date as current event in the row. If yes, then it'll pass without printing the date, and if no, then it'll print it. Before loop gets ended, there is an assignment for $last_event which assigns current passed event for next determination. This is the most simplest form that I could explain.

    Now, if you don't want to guess similar dates in terms of just printing them out, that's another scenario.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化