0

I have an array like this:

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
        )

Etc.

I want to display this information in a table grouped by Date. I'm using foreach to get each of the values but I'm trying to figure out how to examine if the date is the same in each array and only echo the date once if so. This is what I've got so far...

<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>
3
  • If you weren't going to use a for loop, how else do you plan on displaying each row of the table? Your code looks like a pretty good implementation of what you want to accomplish. Commented Nov 30, 2012 at 18:02
  • echo something new line echo something new line echo something new line echo something new line echo something new line echo something new line hurts my feelings. Commented Nov 30, 2012 at 18:02
  • @Jeremy1026 "but I'm trying to figure out how to examine if the date is the same in each array and only echo the date once if so" Commented Nov 30, 2012 at 18:12

3 Answers 3

3

You can only have one thead. Set the date at the end of the loop, or after you've done the following comparison: is this date not equal to the previous one, or not set. If so, output it else output a non-breaking space.

<?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];
} 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

OK, thank you. This is what I was after and wasn't sure how to do it but this makes a lot of sense. Much appreciated.
0

As you are going through the loop, save the previous date, if it is the same then don't echo it, otherwise save the new date as the previous date and echo the new date.

Comments

0

As a solution to your problem please refer the below code snippet.

        <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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.