簡體   English   中英

如何將PHP數據的多維數組轉換為HTML表?

[英]How to convert multi-dimensional array of PHP data to an HTML table?

我正在嘗試使用PHP顯示格式化的HTML時間表。

我想將數據從多維數組輸出到HTML表格,該表格的格式總共為8列(周一至周日每天一列,左側為每個會話的開始時間一列)。 行的數量取決於每天有多少會話。

我的解決方案在一定程度上有效,您可以在下圖中看到輸出,但是您也可以看到由於某種原因生成了額外的行。 無論一天的會話量如何,它總是僅多一行。 當前解決方案

數據表示如下:

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,與星期一相同: 越野車輸出

這是我的越野車解決方案。 我在注釋中標記了我要出錯的行。

    // $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;

問題在於$ 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM