2012-03-08 170 views
0

所以基本上我想要的是用foreach显示一些变量,但它需要使用嵌套循环来完成,因为它有子菜单。嵌套的Foreach循环(PHP)

这是列表:

<whmcsapi> 
<action>getclientsdomains</action> 
<clientid>123</clientid> 
<totalresults>2</totalresults> 
<startnumber>0</startnumber> 
<numreturned>2</numreturned> 
<domains> 
<domain> 
    <id>1</id> 
    <userid>123</userid> 
    <orderid>1</orderid> 
    <regtype>Register</regtype> 
    <domainname>whmcsdomain.com</domainname> 
    <registrar>enom</registrar> 
    <regperiod>1</regperiod> 
    <firstpaymentamount>8.95</firstpaymentamount> 
    <recurringamount>8.95</recurringamount> 
    <paymentmethod>paypal</paymentmethod> 
    <paymentmethodname>Credit Card or Debit Card</paymentmethodname> 
    <regdate>2011-01-01</regdate> 
    <expirydate>2012-01-01</expirydate> 
    <nextduedate>2012-01-01</nextduedate> 
    <status>Active</status> 
    <subscriptionid></subscriptionid> 
    <dnsmanagement></dnsmanagement> 
    <emailforwarding></emailforwarding> 
    <idprotection></idprotection> 
    <donotrenew></donotrenew> 
    <notes></notes> 
</domain> 
... 
</domains> 
</whmcsapi> 

是第一类 第二个。

这是我迄今为止,但没有取得成果:

$command = 'getclientsdomains'; 
$values = array('clientid' => $_SESSION['uid']); 

# Call API 
$results = localAPI($command,$values); 

    foreach ($results as $id => $result) { 

      echo $id . " " . $result ."<br />"; 

      foreach ($result as $domains) { 

      echo $domains; 

      foreach($domains as $key => $value) { 

      echo $key . $value; 

      } 

      } 

     } 

这是输出:

result success 
clientid 1 
domainid 
totalresults 1 
startnumber 0 
numreturned 1 
domains Array 
Array 

在此先感谢。

+0

您是否尝试过在你的数组值使用print_r的数据使用递归函数来循环? - 它会揭示数组的隐藏内容;) – 2012-03-08 23:23:16

回答

0

通过

function print_list($node) { 
    foreach($node as $key => $value) { 
     if(is_array($value)) 
      print_list($value); 
     else 
      echo "$key: $value\n"; 
    } 
} 
+0

工作:)真棒。 另一个问题是,如何只输出1个具体的值? – user1253622 2012-03-08 23:33:02

+0

好吧,如果我理解正确的话,_localAPI()_函数会返回一个数组,所以如果您想从“clientid”中读取值,您可以使用_ $ result [“clientid”] _ – 2012-03-08 23:36:59

+0

这样做这个: 函数get_registrar($ node)foreach($ node = $ key => $ value){ if(is_array($ value)) print_list($ value); else if($ key ==“registrar”){ \t \t \t \t return $ value; \t \t \t \t} } – user1253622 2012-03-08 23:40:57