php - 多维 PHP 数组转换为 HTML 表格

标签 php html css arrays dns

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

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

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

我想在 HTML 表格中显示数据,就像您可以在此处的 DNS 报告部分中看到的那样:https://stackoverflow.com.websiteoutlook.com/

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

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

如能提供有关如何生成此表的任何帮助,我们将不胜感激。

$dns 请求的示例输出:

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>

关于php - 多维 PHP 数组转换为 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676696/

相关文章:

html - CSS悬停子div背景颜色变化

html - 为整个页面添加噪音

html - 如何为每个链接设置不同的悬停颜色

php - 如何提高sql INSERT INTO速度

javascript 网络应用程序开发

php - MySQL Wrapper 和获取行

html - 居中 flex 元素,同时将一个元素放在 Angular 落

html - 悬停时转换 SVG 渐变色标

php - 从php中的电子邮件地址获取用户名

php - Apache ErrorDocument 指令不重定向