php创建动态html表

发布于 2024-10-05 20:11:16 字数 451 浏览 0 评论 0原文

我需要在解析 JSON 文件后使用 PHP 创建一个动态 html 表。
我的表需要这个列结构; 姓名 |状态 |年龄 |计数 |进展|不好

如何为从 JSON 文件解析的每个“记录”创建一个新行(我可以创建一个制表符分隔的字符串)。
另一个困难是某些“记录”仅包含该列的数据所有列的“名称”和其他内容。

所以我的问题是如何动态添加一行到表中并填充右列?
JSON 文件的关键是列标题

JSON 格式示例:

{ 
"John":     {"status":"Wait" }, 
"Jennifer": {"status":"Active" }, 
"James":    {"status":"Active","age":56,"count":10,"progress":0.0029857,"bad":0} 
}

I need to create a dynamic html table using PHP after parsing a JSON file.
I need this column structure for my table; Name | Status | Age | Count | Progress | Bad

How can I create a new row for every 'record' parsed from the JSON file (I can create a tab separated string).
An additional difficulty is that some 'records' only contain data for the column 'Name' and others for all columns.

So my question is how to add a row dynamically to the table and fill the right column ?
(The key form the JSON file is the column header)

Example of JSON format:

{ 
"John":     {"status":"Wait" }, 
"Jennifer": {"status":"Active" }, 
"James":    {"status":"Active","age":56,"count":10,"progress":0.0029857,"bad":0} 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-10-12 20:11:16

像这样的事情会起作用:

$data = json_decode($json_string);
$columns = array();

echo "<table><tbody>";
foreach ($data as $name => $values) {
    echo "<tr><td>$name</td>";
    foreach ($values as $k => $v) {
        echo "<td>$v</td>";
        $columns[$k] = $k;
    }
    echo "</tr>";
}
echo "</tbody><thead><tr><th>name</th>";
foreach ($columns as $column) {
    echo "<th>$column</th>";
}
echo "</thead></table>"

Something like this would work:

$data = json_decode($json_string);
$columns = array();

echo "<table><tbody>";
foreach ($data as $name => $values) {
    echo "<tr><td>$name</td>";
    foreach ($values as $k => $v) {
        echo "<td>$v</td>";
        $columns[$k] = $k;
    }
    echo "</tr>";
}
echo "</tbody><thead><tr><th>name</th>";
foreach ($columns as $column) {
    echo "<th>$column</th>";
}
echo "</thead></table>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文