Php 循环数组以获取数据
我调用API以返回作业列表。我得到的输出如下Php 循环数组以获取数据,php,arrays,Php,Arrays,我调用API以返回作业列表。我得到的输出如下 SimpleXMLElement {#357 ▼ +"Jobs": SimpleXMLElement {#368 ▼ +"Job": array:13 [▼ 0 => SimpleXMLElement {#371 ▼ +"ID": "J000006" +"Name": "HTML Website" +"Client": SimpleXMLElement {#387 ▶}
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 ▶}
]
}
}
Job -> ID
Job -> Name
Job -> Client
Quote -> Amount
Quote -> AmountTax
Quote -> AmountIncludingTax
我现在正试图获得与工作相关的报价。因此,我进行了一个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 ▶}
]
}
}
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++;
}
我现在有两个XMLElements,我现在尝试创建一个数组,它有以下内容
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 ▶}
]
}
}
Job -> ID
Job -> Name
Job -> Client
Quote -> Amount
Quote -> AmountTax
Quote -> AmountIncludingTax
所以我创建了一个空数组
$finalArray = array();
$iterator = 0;
使用上面的XML,可以将引用与作业匹配的是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"
]
]
]
]
有相当多的引用已经被接受,它们与工作名称同名,所以我应该得到所有这些数据
也是
使用上述代码,为什么会覆盖我的数据
谢谢我想你忘了增加你的
$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
,如果您只使用[]
它将自动像这样递增该数组
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 ▶}
]
}
}
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++;
}
我认为您忘记了增加
$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
,如果您只使用[]
它将自动像这样递增该数组
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 ▶}
]
}
}
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++;
}