循环数组获取数据
问题描述:
我打电话给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名称相同的名称,所以我应该得到所有这些数据 。
有了上面的代码,为什么我的数据被覆盖?
感谢
答
我想你忘记增加你的$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++;
}