【问题标题】:Multi-Dimensional PHP Array Into HTML Table将多维 PHP 数组转换为 HTML 表
【发布时间】:2020-02-28 18:45:57
【问题描述】:

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

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

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

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

应该有 5 列。这 4 个变量在每个 DNS 记录中:主机、类型、类、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
            )
    )
)

【问题讨论】:

  • 如果您显示一些来自 $dns 的示例输出会有所帮助
  • @MosheGross 没问题,刚刚为你添加了。

标签: php html css arrays dns


【解决方案1】:

希望下面的代码可以帮到你

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-02-06
  • 2019-09-22
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 2015-11-17
相关资源
最近更新 更多