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