2016-04-21 135 views
-1

我打電話給API以返回作業列表。我得到的輸出如下循環數組獲取數據

SimpleXMLElement {#357 ▼ 
    +"Jobs": SimpleXMLElement {#368 ▼ 
    +"Job": array:13 [▼ 
     0 => SimpleXMLElement {#371 ▼ 
     +"ID": "J000006" 
     +"Name": "HTML Website" 
     +"Client": SimpleXMLElement {#387 ▶} 
     +"Budget": "5000.00" 
     +"State": "In Progress" 
     +"StartDate": "2016-03-31T00:00:00" 
     +"DueDate": "2016-03-31T00:00:00" 
     } 
     1 => SimpleXMLElement {#372 ▶} 
     2 => SimpleXMLElement {#373 ▶} 
     3 => SimpleXMLElement {#374 ▶} 
     4 => SimpleXMLElement {#375 ▶} 
     5 => SimpleXMLElement {#376 ▶} 
    ] 
    } 
} 

我現在試圖獲取與工作有關的報價。所以我做了一個API調用來獲取行情的列表產生這樣的

SimpleXMLElement {#358 ▼ 
    +"Quotes": SimpleXMLElement {#366 ▼ 
    +"Quote": array:12 [▼ 
     0 => SimpleXMLElement {#369 ▼ 
     +"ID": "Q0019" 
     +"Type": "Quote" 
     +"State": "Accepted" 
     +"Name": "HTML Website" 
     +"Budget": "5000.00" 
     +"LeadID": "1232718" 
     +"Date": "2016-04-21T00:00:00" 
     +"ValidDate": "2016-05-19T00:00:00" 
     +"Amount": "1950.00" 
     +"AmountTax": "390.00" 
     +"AmountIncludingTax": "2340.00" 
     +"Client": SimpleXMLElement {#384 ▶} 
     } 
     1 => SimpleXMLElement {#370 ▶} 
     2 => SimpleXMLElement {#371 ▶} 
     3 => SimpleXMLElement {#372 ▶} 
     4 => SimpleXMLElement {#373 ▶} 
     5 => SimpleXMLElement {#374 ▶} 
    ] 
    } 
} 

所以我現在有兩個XMLElements,現在我想創建它具有以下

Job -> ID 
Job -> Name 
Job -> Client 
Quote -> Amount 
Quote -> AmountTax 
Quote -> AmountIncludingTax 

數組所以我創建一個空陣列

$finalArray = array(); 
$iterator = 0; 

通過上面的XML,可以將Quote與Quote匹配的是Name屬性。於是,我開始循環作業和行情,以填補 我的陣列中的數據,我需要

foreach ($currentJobsXML->Jobs->Job as $job) { 
    $seconditerator = 0; 
    foreach($jobsQuoteXML->Quotes->Quote as $quote) { 
     if((string)$quote->State == 'Accepted') { 
      if ((string)$job->Name == (string)$quote->Name) { 
       $finalArray[$iterator]['TEST'][$seconditerator] = array(
        'Job ID' => (string)$job->ID, 
        'Project Name' => (string)$job->Name, 
        'Client' => (string)$job->Client->Name, 
        'Quote Exc VAT' => (string)$quote->Amount, 
        'VAT Amount' => (string)$quote->AmountTax, 
        'Total Amount' => (string)$quote->AmountIncludingTax 
       ); 
       $seconditerator++; 
      } 
     } 
    } 
} 

與上面的代碼,我只似乎得到一個輸出在我的數組

array:1 [▼ 
    0 => array:1 [▼ 
    "TEST" => array:1 [▼ 
     0 => array:6 [▼ 
     "Job ID" => "J000006" 
     "Project Name" => "HTML Website" 
     "Client" => "Prospect 1" 
     "Quote Exc VAT" => "1950.00" 
     "VAT Amount" => "390.00" 
     "Total Amount" => "2340.00" 
     ] 
    ] 
    ] 
] 

有有相當多的報價已被接受與Jobs名稱相同的名稱,所以我應該得到所有這些數據 。

有了上面的代碼,爲什麼我的數據被覆蓋?

感謝

回答

2

我想你忘記增加你的$iterator

foreach ($currentJobsXML->Jobs->Job as $job) { 
    $seconditerator = 0; 
    foreach($jobsQuoteXML->Quotes->Quote as $quote) { 
     if((string)$quote->State == 'Accepted') { 
      if ((string)$job->Name == (string)$quote->Name) { 
       $finalArray[$iterator]['TEST'][$seconditerator] = array(
        'Job ID' => (string)$job->ID, 
        'Project Name' => (string)$job->Name, 
        'Client' => (string)$job->Client->Name, 
        'Quote Exc VAT' => (string)$quote->Amount, 
        'VAT Amount' => (string)$quote->AmountTax, 
        'Total Amount' => (string)$quote->AmountIncludingTax 
       ); 
       $seconditerator++; 
      } 
     } 
    } 
    $iterator++; 
} 

我不真的覺得你真的很需要那$seconditerator或者,如果你只是用[]它會自動增加該數組像這樣

foreach ($currentJobsXML->Jobs->Job as $job) { 
    //$seconditerator = 0; 
    foreach($jobsQuoteXML->Quotes->Quote as $quote) { 
     if((string)$quote->State == 'Accepted') { 
      if ((string)$job->Name == (string)$quote->Name) { 
       //$finalArray[$iterator]['TEST'][$seconditerator] = array(
       $finalArray[$iterator]['TEST'][] = array(
        'Job ID' => (string)$job->ID, 
        'Project Name' => (string)$job->Name, 
        'Client' => (string)$job->Client->Name, 
        'Quote Exc VAT' => (string)$quote->Amount, 
        'VAT Amount' => (string)$quote->AmountTax, 
        'Total Amount' => (string)$quote->AmountIncludingTax 
       ); 
       //$seconditerator++; 
      } 
     } 
    } 
    $iterator++; 
}