我试图将列表中的每个子域名与其主域名相关联,我这样做:
<?php
$main_domains = ['example.co.uk','example.com','example.org'];
$subdomains = ['sub.example.com','ftp.example.org','ftp.example.co.uk','mail.example.com'];
$results = [];
foreach ($subdomains as $subdomain) {
foreach ($main_domains as $domain) {
if (strpos($subdomain, $domain) !== false) {
$results[$domain][] = $subdomain;
continue;
}
}
}
其中最后$results
包含以下内容:
array(3) {
["example.com"]=>
array(2) {
[0]=>
string(15) "sub.example.com"
[1]=>
string(16) "mail.example.com"
}
["example.org"]=>
array(1) {
[0]=>
string(15) "ftp.example.org"
}
["example.co.uk"]=>
array(1) {
[0]=>
string(17) "ftp.example.co.uk"
}
}
然而,我现在想回显信息,如下所示:
<main domain> <sub domain>
<main domain> <sub domain>
...
即:
example.com sub.example.com
example.com mail.example.com
example.org ftp.example.org
example.co.uk ftp.example.co.uk
我该怎么做呢?
最重要的是设置初始结构。
$dom=array('example.co.uk'=>array('ftp.example.co.uk','zzz.example.co.uk'));
foreach($dom as $domain=>$sub){
foreach($sub as $s){
echo $domain.' '.$s."n";
}
}
输出:example.co。英国ftp.example.co。Uk
example.co.uk
正是你想要的