简体   繁体   中英

generate html table from json data using php

I have this as input data how do I convert it to a html table using the keys as the header row and then looping the values for the rest of the rows?

my input json data can be downloaded from 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
        )

my PHP so far is...

<?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>";
?>

Provided every record has the same keys, you could just get all the keys with the first record and then look through each object for all the values like:

    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>";
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM