簡體   English   中英

如何使用 PHP 將這些 JSON 數據轉換為這樣的 HTML 表

[英]How do I get this JSON data into HTML table like this using PHP

我已經知道如何連接到數據庫等等。 我只需要知道我需要什么樣的 PHP 循環才能正確獲取這些數據

這是示例 JSON:

[equipment] => Array
    (
        [0] => Array
            (
                [id] => 401582887101
                [name] => Driver Seat
                [equipmentType] => OTHER
                [availability] => STANDARD
                [attributes] => Array
                    (
                        [0] => Array
                            (
                                [name] => Number Of Driver Seat Manual Adjustments
                                [value] => 6
                            )

                        [1] => Array
                            (
                                [name] => Height Adjustable Driver Seat
                                [value] => height adjustable
                            )

                    )

            )

這是我希望表格的樣子:

id  name    equipmentType   availibility    attribute name  attribute value

401582887101 駕駛員座椅 OTHER STANDARD 駕駛員座椅手動調節次數 6 401582887101 駕駛員座椅 OTHER STANDARD Height Adjustable 駕駛員座椅高度可調

它有點丑陋的代碼,但它適用於您的情況,只要數組的數據結構保持不變:

echo '<table border="1">';
echo '<thead><th>id</th><th>name</th><th>equipmentType</th><th>availibility</th><th>attribute name</th><th>attribute value</th></thead>';
foreach ($data['equipment'] as $equipment) {
    $row = '<tr>';
    foreach($equipment as $key => $item) {
        if(is_array($item)) {
            foreach ($item as $attribute) {
                $attributeStr = '';
                $attributeStr .= '<td>'.$attribute['name'].'</td>';
                $attributeStr .= '<td>'.$attribute['value'].'</td>';
                echo $row.$attributeStr.'</tr>';
            }
        }
        else {
            $row .= '<td>'.$item.'</td>';
        }
    }
}
echo '</table>';

暫無
暫無

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

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