PHP 多维 JSON 数组到 HTML 表

我有一个多维JSON数组,我想生成一个在这个数据中基本的HTML表。

JSON 采用以下格式 - 此处链接;

[

    {

        "id": 1,

        "date": "10.02.2020",

        "floor": "Ground",

        "room_name": "G01",

        "room_status": 1,

        "hour": {

            "08": 1,

            "09": 2,

            "10": 1,

            "11": 2,

            "12": 0,

            "13": 1,

            "14": 2,

            "15": 2,

            "16": 1,

            "17": 0,

            "18": 1,

            "19": 1,

            "20": 0

        }

    },

    {

        "id": 2,

        "date": "10.02.2020",

        "floor": "Ground",

        "room_name": "G02",

        "room_status": 2,

        "hour": {

            "08": 1,

            "09": 1,

            "10": 0,

            "11": 0,

            "12": 0,

            "13": 0,

            "14": 1,

            "15": 1,

            "16": 0,

            "17": 1,

            "18": 1,

            "19": 1,

            "20": 1

        }

    },

    {

        "id": 3,

        "date": "10.02.2020",

        "floor": "Ground",

        "room_name": "G03",

        "room_status": 1,

        "hour": {

            "08": 0,

            "09": 2,

            "10": 2,

            "11": 2,

            "12": 0,

            "13": 2,

            "14": 1,

            "15": 0,

            "16": 1,

            "17": 1,

            "18": 1,

            "19": 2,

            "20": 2

        }

    },

    {

        "id": 4,

        "date": "10.02.2020",

        "floor": "Ground",

        "room_name": "G04",

        "room_status": 2,

        "hour": {

            "08": 1,

            "09": 2,

            "10": 2,

            "11": 1,

            "12": 1,

            "13": 0,

            "14": 0,

            "15": 1,

            "16": 0,

            "17": 2,

            "18": 0,

            "19": 1,

            "20": 2

        }

    },

   

我需要遍历此数据并在左侧显示 ,and 列。我可以这样做,但是我还需要在同一表格单元格中显示 和 。我不知道如何实现这一点。datefloorroom_namehourroom_status


我需要我的最终表看起来像这样(请注意,我将在最后一个表中将整数更改为字符串,即,等);0 = false1 = true


繁花如伊
浏览 133回答 3
3回答

MYYA

我想这就是你在寻找的:$string = file_get_contents("data.json");$json = json_decode($string, true);<?php foreach($json as $room): ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['date']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['floor']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['room_name']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($room['hour'] as $hour => $status): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $hour; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $status; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; </tr><?php endforeach; ?>

海绵宝宝撒

有更好的方法来制作桌子,但这应该实现你正在寻找的东西;$string = file_get_contents("data.json");$json = json_decode($string, true);<?php foreach($json as $room): ?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['date']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['floor']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php echo $room['room_name']; ?></p>&nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($room["hour"] as $hour => $status){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<td>" $hour . "<br/>" . $status . "</td>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; </tr><?php endforeach; ?>

手掌心

foreach( $room['hour'] as $hour)&nbsp;{...}等等...嵌套在你的前方
打开App,查看更多内容
随时随地看视频慕课网APP