0

我有一个这样的数组:

Array
(
    [0] => Array
        (
            [date] => 2012-11-29
            [description] => Work Done
            [hours] => 1.0
            [id] => 6502
            [person-id] => 8853
            [project-id] => 8928
            [todo-item-id] => 122353
            [person-name] => Jane Doe
        )

    [1] => Array
        (
            [date] => 2012-11-29
            [description] => Work Done 2
            [hours] => 1.0
            [id] => 6502
            [person-id] => 8853
            [project-id] => 8928
            [todo-item-id] => 122353
            [person-name] => Jane Doe
        )

    [2] => Array
        (
            [date] => 2012-11-28
            [description] => Work Done 3
            [hours] => 1.0
            [id] => 6502
            [person-id] => 8853
            [project-id] => 8928
            [todo-item-id] => 122353
            [person-name] => Jane Doe
        )

等等。

我想在按日期分组的表格中显示此信息。我正在使用 foreach 来获取每个值,但我试图弄清楚如何检查每个数组中的日期是否相同,如果是,则只回显一次日期。这就是我到目前为止所得到的......

<table>
     <tbody>
                <?php 

                  foreach($time as $log){
                    //tried this but it doesn't work 
                    //if(array_unique($log['date'])){
                      echo '<thead>
                              <tr class="info">
                                <td colspan="3">'.$log[date].'</td>
                              </tr>
                            </thead>';
                          //};


                  echo "<tr>";
                  echo "<td>{$log['todo-item-id']}</td>";
                  echo "<td>{$log['description']}</td>";
                  echo "<td>{$log['hours']}</td>";
                  echo "</tr>";
                } ?>

              </tbody>
</table>
4

3 回答 3

3

你只能有一个头。在循环结束时设置日期,或者在您完成以下比较之后:此日期是否不等于前一个日期,或未设置。如果是,则输出它,否则输出一个不间断的空格。

<?php 

foreach($time as $log) {
    // check if it is not set or not equal and output if needed
    echo '<tr class="info">
            <td colspan="3">'.(!isset($prevdate) || $prevdate!=$log[date]? $log[date] : '&nbsp;').'</td>
          </tr>
        ';

    echo "<tr>
        <td>{$log['todo-item-id']}</td>
        <td>{$log['description']}</td>
        <td>{$log['hours']}</td>
        </tr>";
    // set it
    $prevdate=$log[date];
} 
?>
于 2012-11-30T18:06:25.887 回答
0

在循环过程中,保存上一个日期,如果相同则不要回显它,否则将新日期保存为上一个日期并回显新日期。

于 2012-11-30T18:03:57.297 回答
0

作为您的问题的解决方案,请参考以下代码片段。

        <table>
            <tbody>
            <?php 
    $a=array();
              foreach($time as $log){

                if(!in_array($log['date'],$a)) { array_push($a,$log['date']);

                  echo '<thead>
                          <tr class="info">
                            <td colspan="3">'.$log[date].'</td>
                          </tr>
                        </thead>';
                      }


              echo "<tr>";
              echo "<td>{$log['todo-item-id']}</td>";
              echo "<td>{$log['description']}</td>";
              echo "<td>{$log['hours']}</td>";
              echo "</tr>";
            } ?>

          </tbody>
    </table>
于 2012-11-30T19:12:22.847 回答