2012-03-08 6 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> 

첫 번째 카테고리 하나이다 :

목록이다.

이 내가 결과없이 지금까지하지만 달성 한 것입니다 : 사전에

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

감사 :

$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; 

      } 

      } 

     } 

이 출력됩니다.

+0

당신이 당신의 배열 값에 print_r의 사용하려고 했 데이터를 통해 루프 재귀 함수를 사용? - 그것은 배열의 숨겨진 내용을 공개합니다;) –

답변

0

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

Working :) Awesome. 또 다른 질문은, 단지 1 개의 특정 값을 출력하는 방법입니까? – user1253622

+0

글쎄, 알았다면 _localAPI() _ 함수는 배열을 반환하므로 "clientid"에서 값을 읽으려면 _ $ result [ "clientid"] _ –

+0

을 사용하면됩니다. 이 : 함수 get_registrar ($ node) { foreach ($ node as $ key => $ value) { if (is_array ($ value)) print_list ($ value); else if ($ key == "registrar") { \t \t \t \t return $ value; \t \t \t \t – user1253622