如何将PHP数据的多维数组转换为HTML表格?

问题描述:

我想使用PHP显示格式化的HTML时间表。如何将PHP数据的多维数组转换为HTML表格?

我想将数据从多维数组输出到格式为总共8列的HTML表格中(每天的列为Mon-Sun,左边为显示每个会话的开始时间的列)。 行数取决于每天有多少会话。

我的解决方案在某种程度上可行,您可以在下图中看到输出,但您也可以看到由于某种原因生成了额外的行。无论一天的会话数量如何,总是只有一行。 Current Solution

的数据是这样表示:

Array 
(
[0] => Array 
    (
     [0] => Array 
      (
       [id] => 1 
       [name] => A Monday Session 
       [start_time] => 10:00 
      ) 

     [1] => Array 
      (
       [id] => 5 
       [name] => Another Monday Session 
       [start_time] => 11:00 
      ) 

     [2] => Array 
      (
       [id] => 6 
       [name] => Yet Another Monday Session 
       [start_time] => 12:00 
      ) 

    ) 

[1] => Array 
    (
     [0] => Array 
      (
      ) 

    ) 

[2] => Array 
    (
     [0] => Array 
      (
       [id] => 8 
       [name] => A Wednesday Session 
       [start_time] => 14:30 
      ) 

    ) 

[3] => Array 
    (
     [0] => Array 
      (
       [id] => 3 
       [name] => A Thursday Session 
       [start_time] => 09:00 
      ) 

    ) 

[4] => Array 
    (
     [0] => Array 
      (
      ) 

    ) 

[5] => Array 
    (
     [0] => Array 
      (
      ) 

    ) 

[6] => Array 
    (
     [0] => Array 
      (
       [id] => 4 
       [name] => A Sunday Session 
       [start_time] => 13:00 
      ) 

    ) 

) 

正如你所看到的主键表示一周的日子。 0 =星期一,1 =星期二等。 每天有一个会话列表。在这个例子中,星期一有3个会话,周三,周四,周日也有一个。

请注意,它们都有不同的起始时间。将会话引入具有重复开始时间的数据的那一刻,也会创建额外的行,而不是共享同一行。周四会议的输出改为10:00,与星期一相同: Buggy Output

这里是我的越野车解决方案。我在评论中标出了我要出错的地方。

// $sessionRows is the array of arrays containing the data above. 

    // Grabs array of session times from $sessionRows that has been sorted and duplicates removed. 
    // Current values: Array ([0] => 09:00 [1] => 10:00 [2] => 11:00 [3] => 12:00 [4] => 13:00 [5] => 14:30) 
    $sessionTimes = $this->getSessionTimes($days); 

    $numOfRows = count($sessionTimes); 
    $numOfCols = $dayIdx; 

    // Create grid with correct dimensions and rows represented by the session times 
    $grid = array(); 
    for($i=0;$i<count($sessionTimes);$i++) { 
     $row = array(); 
     for($j=0;$j<$numOfCols;$j++) { 
      $row[] = '<td></td>'; 
     } 
     $grid[$sessionTimes[$i]] = $row; 
    } 


    // Populate grid with session info added to correct coordinates. 
    for($i=0;$i<$numOfCols;$i++) { 
     echo count($sessionRows[$i]); 
     for($j=0;$j<count($sessionRows);$j++) { 
      $grid[$sessionRows[$i][$j]['start_time']][$i] = $sessionRows[$i][$j]; 
     } 
    } 

    $rows=''; 
    $idx = 0; 
    foreach($grid as $rowArray) { 
     $rows .= '<tr>'; 
     /*** This lines is the problem! It adds the session time as the first column. ***/ 
     $rows .= '<td class="time-col">'.$sessionTimes[$idx].'</td>'; 
     for($i=0;$i<count($rowArray);$i++) { 
      if(!empty($rowArray[$i]['name'])){ 
       $rows .= '<td>'.$rowArray[$i]['name'].'<br>'.$rowArray[$i]['start_time'].'</td>'; 
      } else { 
       $rows .= '<td> - </td>'; 
      } 
     } 
     $rows .= '</tr>'; 
     $idx++; 
    } 

return $rows; 
+0

你怎么知道一个星期的一天? – Misunderstood

+0

只是因为阵列的排列方式。 0 =星期一,1 =星期二等 – Muzzstick

问题是$ sessionTimes数组使用array_unique()函数删除了重复项。

此功能保留已删除值的索引。所以额外的行正在创建没有价值。

将此更改为array_values(array_unique($ array))允许重新生成密钥并解决问题。

感谢任何看过它的人,希望我早点认识到这一点!

这是一个使用较少循环的替代解决方案。

它所做的是循环遍历时间,然后遍历每一天以确定会话start_time是否匹配,如果是,则将其添加到输出表。

<?php 

$sessionTimes = ['09:00', '10:00', '11:00', '12:00', '13:00', '14:30']; 

$schedule = [ 
    [ 
     [ 
      'id' => 1, 
      'name' => 'A Monday Session', 
      'start_time' => '10:00' 

     ], 
     [ 
      'id' => 5, 
      'name' => 'Another Monday Session', 
      'start_time' => '11:00' 

     ], 
     [ 
      'id' => 6, 
      'name' => 'Yet Another Monday Session', 
      'start_time' => '12:00' 

     ], 
    ], 
    [ 
     []  
    ], 
    [ 
     [ 
      'id' => 8, 
      'name' => 'A Wednesday Session', 
      'start_time' => '14:30' 
     ]  
    ], 
    [ 
     [ 
      'id' => 3, 
      'name' => 'A Thursday Session', 
      'start_time' => '09:00' 
     ]  
    ], 
    [ 
     [] 
    ], 
    [ 
     [] 
    ], 
    [ 
     [ 
      'id' => 4, 
      'name' => 'A Sunday Session', 
      'start_time' => '13:00' 
     ] 
    ] 
]; 

$output = '<table><thead><tr><th></th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th><th>Sunday</th></thead><tbody>'; 

foreach ($sessionTimes as $sessionTime) { 
    $output .= '<tr><td>'.$sessionTime.'</td>'; 

    $day = 0; 
    for ($i = 0; $i < count($schedule); $i++) { 
     if ($i == $day) { 
      $sessionAtTime = '<td>-</td>'; 
      foreach ($schedule[$day] as $session) { 
       if (!empty($session) && $session['start_time'] == $sessionTime) { 
        $sessionAtTime = '<td><b>' . $session['name'] . '</b><br>Start: ' . $sessionTime.'</td>'; 
       } 
      } 
      $output .= $sessionAtTime; 
      $day++; 
     } 
    } 

    $output .= '</tr>'; 
} 

$output .= '</tbody></table>'; 

echo $output;