繁体   English   中英

多维 PHP 阵列到 HTML 表中

[英]Multi-Dimensional PHP Array Into HTML Table

我正在尝试在 HTML 表中显示特定域的所有 DNS 记录。

这是显示记录的当前代码:

<?php
$dns = dns_get_record("stackoverflow.com");
print_r($dns);
?>

I want to display the data in an HTML table, like you can see in the DNS Report section here: https://stackoverflow.com.websiteoutlook.com/

应该有 5 列。 这 4 个变量在每个 DNS 记录中:主机、类型、Class、TTL。 然后每条记录都会有一个或多个我想显示的“额外”变量,就像上面的链接一样。

我已经阅读了一些关于使用foreach语句进行循环的教程。 但是,我可以看到的所有示例要么具有硬编码变量,要么是多级 arrays,要么没有在某些 arrays 中可能存在的“额外”字段,但在其他示例中不存在。

任何有关如何生成此表的帮助将不胜感激。

按要求提供$dns的 Output 示例:

Array
(
[0] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => A
        [ip] => 151.101.1.69
    )

[1] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => A
        [ip] => 151.101.65.69
    )

[2] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => A
        [ip] => 151.101.129.69
    )

[3] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => A
        [ip] => 151.101.193.69
    )

[4] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 172800
        [type] => NS
        [target] => ns-358.awsdns-44.com
    )

[5] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 172800
        [type] => NS
        [target] => ns-1033.awsdns-01.org
    )

[6] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 172800
        [type] => NS
        [target] => ns-cloud-e1.googledomains.com
    )

[7] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 172800
        [type] => NS
        [target] => ns-cloud-e2.googledomains.com
    )

[8] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 21600
        [type] => SOA
        [mname] => ns-cloud-e1.googledomains.com
        [rname] => cloud-dns-hostmaster.google.com
        [serial] => 1
        [refresh] => 21600
        [retry] => 3600
        [expire] => 259200
        [minimum-ttl] => 300
    )

[9] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => MX
        [pri] => 1
        [target] => aspmx.l.google.com
    )

[10] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => MX
        [pri] => 5
        [target] => alt1.aspmx.l.google.com
    )

[11] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => MX
        [pri] => 5
        [target] => alt2.aspmx.l.google.com
    )

[12] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => MX
        [pri] => 10
        [target] => alt3.aspmx.l.google.com
    )

[13] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => MX
        [pri] => 10
        [target] => alt4.aspmx.l.google.com
    )

[14] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => TXT
        [txt] => MS=ms52592611
        [entries] => Array
            (
                [0] => MS=ms52592611
            )
    )

[15] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => TXT
        [txt] => google-site-verification=ctogLnZNAdc_CXq8yOhODMLpmugGynjxKecKHDz4oL8
        [entries] => Array
            (
                [0] => google-site-verification=ctogLnZNAdc_CXq8yOhODMLpmugGynjxKecKHDz4oL8
            )
    )

[16] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => TXT
        [txt] => google-site-verification=o3EMam8yBGo1yEjyybIiZcOunGHOQKpo8JmOtp9n1BU
        [entries] => Array
            (
                [0] => google-site-verification=o3EMam8yBGo1yEjyybIiZcOunGHOQKpo8JmOtp9n1BU
            )
    )

[17] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => TXT
        [txt] => google-site-verification=rdWtMbplKjbRHGr2dNONfwkqithlUvjr3u6i8QEz_mo
        [entries] => Array
            (
                [0] => google-site-verification=rdWtMbplKjbRHGr2dNONfwkqithlUvjr3u6i8QEz_mo
            )
    )

[18] => Array
    (
        [host] => stackoverflow.com
        [class] => IN
        [ttl] => 300
        [type] => TXT
        [txt] => v=spf1 ip4:198.252.206.0/24 ip4:192.111.0.0/24 ip4:52.38.191.241 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 include:_spf1.stackoverflow.com ~all
        [entries] => Array
            (
                [0] => v=spf1 ip4:198.252.206.0/24 ip4:192.111.0.0/24 ip4:52.38.191.241 ip4:35.190.247.0/24 ip4:64.233.160.0/19 ip4:66.102.0.0/20 ip4:66.249.80.0/20 ip4:72.14.192.0/18 ip4:74.125.0.0/16 ip4:108.177.8.0/21 ip4:173.194.0.0/16 include:_spf1.stackoverflow.com ~all
            )
    )
)

我希望下面的代码可以帮助你

<?php
$dns = dns_get_record("stackoverflow.com");
?>

<table>
    <tr>
        <th>Host</th>
        <th>Type</th>
        <th>Class</th>
        <th>TTL</th>
        <th>Extra</th>
    </tr>
    <?php
        foreach($dns as $dnsset) {
            ?>
            <tr>
                <td><?php echo $dnsset["host"] ?></td>
                <td><?php echo $dnsset["type"] ?></td>
                <td><?php echo $dnsset["class"] ?></td>
                <td><?php echo $dnsset["ttl"] ?></td>
                <td>
                    <?php 
                        foreach($dnsset as $extrasetkey => $extrasetvalue) {
                            if(!in_array($extrasetkey, ['host', 'type', 'class', 'ttl'])) {
                                // If your data has extrasetvalue as array type also add conditions
                                // here and loop through them also
                                echo $extrasetkey . ":" . $extrasetvalue . "<br/>";
                            }
                        }
                    ?>
                </td>
            </tr>
            <?
        }
    ?>
</table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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