2016-01-17 34 views
-1

我有這樣迴路JSON PHP有有2個數據

{"host":[{"name":"*","type":"A","address":"89.163.141.230","hostid":"391162691"},{"name":"www","type":"A","address":"89.163.141.230","hostid":"391453684"}],"DomainServices":{"EmailForwarding":"True","HostRecords":"True"},"Command":"GETHOSTS","APIType":"API","Language":"eng","ErrCount":"0","ResponseCount":"0","MinPeriod":"1","MaxPeriod":"10","Server":"SJL0VWAPI12","Site":"whois","IsLockable":"True","IsRealTimeTLD":"True","TimeDifference":"+08.00","ExecTime":"0.064","Done":"true","RequestDateTime":"1\/17\/2016 5:49:26 AM","debug":{}} 

這2個數據JSON數據,你可以看到它有, 我的PHP代碼:

<?php 
$array = file_get_contents('example.com/api.php?whois=facebook.com'); 
$decode = json_decode($array, true); 
echo $decode['host']['address']; 
?> 

,但它只是呼應1數據。如何循環它?

+0

你可以發佈什麼$解碼包含? –

回答

0

我並不確切知道要打印出來的json字符串的哪一部分,但是如果您想要顯示數組中的每個主機的數據,只需執行類似的操作即可。

$host = $decode['host']; 
foreach ($host as $subKey => $subArray){ 
     echo "Host " . $subKey . ":<br>"; 
     foreach ($subArray as $key => $value){ 
       echo $key . " = " . $value . "<br>"; 
     } 
     echo "<br><br>"; 
} 

輸出將是:

Host 0: 
name = * 
type = A 
address = 89.163.141.230 
hostid = 391162691 


Host 1: 
name = www 
type = A 
address = 89.163.141.230 
hostid = 391453684