2012-12-02 49 views
1

我有一些假設打印域名服務器的代碼。然而在印刷它們時它給了我這樣的:使用dns_get_record()的結果

array(5) { 
    [0]=> 
    array(5) { 
    ["host"]=> 
    string(7) "php.net" 
    ["type"]=> 
    string(2) "NS" 
    ["target"]=> 
    string(19) "remote1.easydns.com" 
    ["class"]=> 
    string(2) "IN" 
    ["ttl"]=> 
    int(32) 
    } 
    [1]=> 
    array(5) { 
    ["host"]=> 
    string(7) "php.net" 
    ["type"]=> 
    string(2) "NS" 
    ["target"]=> 
    string(19) "remote3.easydns.com" 
    ["class"]=> 
    string(2) "IN" 
    ["ttl"]=> 
    int(32) 
    } 
    [2]=> 
    array(5) { 
    ["host"]=> 
    string(7) "php.net" 
    ["type"]=> 
    string(2) "NS" 
    ["target"]=> 
    string(15) "ns2.easydns.com" 
    ["class"]=> 
    string(2) "IN" 
    ["ttl"]=> 
    int(32) 
    } 
    [3]=> 
    array(5) { 
    ["host"]=> 
    string(7) "php.net" 
    ["type"]=> 
    string(2) "NS" 
    ["target"]=> 
    string(15) "ns1.easydns.com" 
    ["class"]=> 
    string(2) "IN" 
    ["ttl"]=> 
    int(32) 
    } 
    [4]=> 
    array(5) { 
    ["host"]=> 
    string(7) "php.net" 
    ["type"]=> 
    string(2) "NS" 
    ["target"]=> 
    string(19) "remote2.easydns.com" 
    ["class"]=> 
    string(2) "IN" 
    ["ttl"]=> 
    int(32) 
    } 
} 

我知道這是一個數組,但是我只希望它打印每個域名服務器

remote2.easydns.com 
ns1.easydns.com 
ns2.easydns.com 
remote1.easydns.com 

這是我當前的代碼:

<?php 
$result = dns_get_record("php.net", DNS_NS); 
echo '<pre>'; 
var_dump($result); 
echo '</pre>'; 
?> 

Print_r做了一件非常相似的事情。

回答

3

假設你的數組變量名$array

foreach($array as $key=>$value){ 
    echo $value['target'].'<br>'; 
} 
+0

'主機'鍵將輸出'php.net'的所有結果。你想輸出'target'鍵。 –

+0

是的,這是一個愚蠢的錯誤。謝謝。 –

+0

天哪不知道我爲什麼這麼想。我現在試圖使用gethostbyname()將主機名解析爲IP,但它僅打印:pastebin.com/1Gs1kyY4而不是完整的IP。我在這裏做錯了什麼? pastebin.com/9u5Q9C79 – user1372896

2

$result是結果的數組。遍歷數組並輸出每個子數組的關鍵字target

$result = dns_get_record("php.net", DNS_NS); 
foreach ($result as $record) { 
    echo $record['target'], "\n"; 
} 
+0

奇怪的新行看起來不起作用,它全部打印在一行上。 – user1372896

+0

新行將打印在源代碼中,而不是在屏幕上 –

+0

天哪不知道爲什麼我認爲哈。我現在試圖使用gethostbyname()將主機名解析爲IP,但它僅打印:http://pastebin.com/1Gs1kyY4而不是完整的IP。我在這裏做錯了什麼? http://pastebin.com/9u5Q9C79 – user1372896