简体   繁体   English

PHP一维数组

[英]php single dimensional array

I'm trying to output an array containing events which is ical file output to be precise. 我正在尝试输出包含事件的数组,确切地说是ical文件输出。 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循环遍历其记录并输出所有事件:

    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 我希望有更简单的方法来达到描述的效果

<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 . 有一个$last_event变量,它将保存通过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语句,该语句确定$last_event是否与该行中的当前事件具有相同的日期。 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. 在循环结束之前,有一个$last_event分配,它分配当前传递的事件用于下一个确定。 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. 现在,如果您不想仅仅打印出来就猜测相似的日期,那是另一种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM