我有这个作为输入数据如何使用键作为标题行将其转换为html表,然后循环其余行的值?
我的输入json数据可以从https://bpaste.net/show/c01e97e208de
下载Array
(
[0] => Array
(
[DIR] => IN
[RULESET] => BLACKLIST
[IN] => eth0.2
[OUT] => br-lan
[MAC] => 10:fe:ed:ff:ea:4c:00:01:5c:98:96:46:08:00:45:00:00:28
[SRC] => 185.56.82.18
[DST] => 192.168.2.163
[LEN] => 40
[TOS] => 0x00
[PREC] => 0x00
[TTL] => 242
[ID] => 1061
[PROTO] => TCP
[SPT] => 57547
[DPT] => 445
[WINDOW] => 1024
[RES] => 0x00
[FLAG] => SYN
[URGP] => 0
)
[1] => Array
(
[DIR] => IN
[RULESET] => CUSTOMLIST
[IN] => eth0.2
[OUT] => br-lan
[MAC] => 10:fe:ed:ff:ea:4c:00:01:5c:98:96:46:08:00:45:00:00:28
[SRC] => 191.101.167.246
[DST] => 192.168.2.163
[LEN] => 40
[TOS] => 0x00
[PREC] => 0x00
[TTL] => 239
[ID] => 6538
[PROTO] => TCP
[SPT] => 55876
[DPT] => 6666
[WINDOW] => 1024
[RES] => 0x00
[FLAG] => SYN
[URGP] => 0
)
[2] => Array
(
[DIR] => IN
[RULESET] => BLACKLIST
[IN] => eth0.2
[OUT] => br-lan
[MAC] => 10:fe:ed:ff:ea:4c:00:01:5c:98:96:46:08:00:45:00:00:28
[SRC] => 121.113.201.196
[DST] => 192.168.2.10
[LEN] => 40
[TOS] => 0x00
[PREC] => 0x00
[TTL] => 49
[ID] => 34152
[PROTO] => TCP
[SPT] => 20281
[DPT] => 23
[WINDOW] => 47916
[RES] => 0x00
[FLAG] => SYN
[URGP] => 0
)
[3] => Array
(
[DIR] => IN
[RULESET] => BLACKLIST
[IN] => eth0.2
[OUT] => br-lan
[MAC] => 10:fe:ed:ff:ea:4c:00:01:5c:98:96:46:08:00:45:00:00:28
[SRC] => 209.126.136.4
[DST] => 192.168.2.163
[LEN] => 40
[TOS] => 0x00
[PREC] => 0x00
[TTL] => 239
[ID] => 54321
[PROTO] => TCP
[SPT] => 50352
[DPT] => 21
[WINDOW] => 65535
[RES] => 0x00
[FLAG] => SYN
[URGP] => 0
)
到目前为止我的PHP是......
<?php
$json=file_get_contents("my_url/firelog.json");
$data=json_decode($json,true);
//print_r($data);
//echo gettype($data), "\n";
echo "<table><tbody>";
if (is_array($data)) {
foreach($data as $key){
if (is_array($key)) {
foreach($key as $k => $v) {
echo "<tr>";
echo "</tr>";
}
}
}
}
$echo "</tbody></table>";
?>
答案 0 :(得分:0)
如果每个记录都有相同的键,您可以使用第一个记录获取所有键,然后查看每个对象的所有值,如:
if (is_array($data)) {
$first = true;
foreach($data as $key){
if (is_array($key)) {
if ($first) {
echo '<tr>';
$first = false;
foreach($key as $k => $v) {
echo '<th>'.$k.'</th>';
}
echo '</tr>';
}
echo "<tr>";
foreach($key as $k => $v) {
echo "<td>".$v."</td>";
}
echo "</tr>";
}
}
}