我正在使用基于查询返回一组发票的API - 这是一个返回6个发票的结果示例,我将其存储在$发票变量中:
SimpleXMLElement Object
(
[Id] => 11109617-58f0-4eff-8f95-9663a2ddeb2f
[Status] => OK
[ProviderName] => Xero Demo
[DateTimeUTC] => 2017-04-04T23:31:03.2920449Z
[Invoices] => SimpleXMLElement Object
(
[Invoice] => Array
(
[0] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => 0e72016e-7c60-4a19-b8d0-1d2c58cc0b49
[Name] => Metfill Constructions
)
[Date] => 2017-03-02T00:00:00
[DueDate] => 2017-03-16T00:00:00
[Status] => AUTHORISED
[LineAmountTypes] => Exclusive
[LineItems] => SimpleXMLElement Object
(
[LineItem] => SimpleXMLElement Object
(
[ItemCode] => PR1113
[Description] => Acme Doodles
[UnitAmount] => 645.00
[TaxType] => OUTPUT
[TaxAmount] => 64.50
[LineAmount] => 645.00
[AccountCode] => 200
[Quantity] => 1.0000
[LineItemID] => 5a3da2f4-271a-4bdb-aeb9-fca65c4faa90
)
)
[SubTotal] => 645.00
[TotalTax] => 64.50
[Total] => 709.50
[UpdatedDateUTC] => 2017-03-01T20:45:01.5
[CurrencyCode] => AUD
[Type] => ACCREC
[InvoiceID] => 5202c206-30d7-4792-8a37-32d1d8bee79f
[InvoiceNumber] => IV1950
[Payments] => SimpleXMLElement Object
(
[Payment] => SimpleXMLElement Object
(
[PaymentID] => b443039a-409e-47cf-a5d9-4cde1640269d
[Date] => 2017-03-02T00:00:00
[Amount] => 500.00
[Reference] => test
[CurrencyRate] => 1.000000
)
)
[AmountDue] => 209.50
[AmountPaid] => 500.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
[1] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => 0e72016e-7c60-4a19-b8d0-1d2c58cc0b49
[Name] => Metfill Constructions
)
[Date] => 2017-03-06T00:00:00
[DueDate] => 2017-03-20T00:00:00
[Status] => AUTHORISED
[LineAmountTypes] => NoTax
[LineItems] => SimpleXMLElement Object
(
[LineItem] => SimpleXMLElement Object
(
[ItemCode] => PR1113
[Description] => Acme Doodles
[UnitAmount] => 645.00
[TaxType] => NONE
[TaxAmount] => 0.00
[LineAmount] => 645.00
[AccountCode] => 200
[Quantity] => 1.0000
[LineItemID] => bc3f2602-b712-49c8-8696-3af1ce589b05
)
)
[SubTotal] => 645.00
[TotalTax] => 0.00
[Total] => 645.00
[UpdatedDateUTC] => 2017-03-05T19:10:52.57
[CurrencyCode] => AUD
[Type] => ACCREC
[InvoiceID] => 50f95c0b-5498-47b9-8034-9f1ed11d759e
[InvoiceNumber] => IV1951
[Payments] => SimpleXMLElement Object
(
[Payment] => SimpleXMLElement Object
(
[PaymentID] => 3f5a4d70-20d2-47cc-a661-3a528958c801
[Date] => 2017-03-06T00:00:00
[Amount] => 380.00
[Reference] => cash
[CurrencyRate] => 1.000000
)
)
[AmountDue] => 265.00
[AmountPaid] => 380.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
[2] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => 2842ce37-86e0-4e89-936b-e1138e79dcf7
[Name] => Swindon College
)
[Date] => 2017-03-06T00:00:00
[DueDate] => 2017-03-20T00:00:00
[Status] => PAID
[LineAmountTypes] => NoTax
[LineItems] => SimpleXMLElement Object
(
[LineItem] => Array
(
[0] => SimpleXMLElement Object
(
[ItemCode] => DevD
[Description] => Development work - developer onsite per day
[UnitAmount] => 650.00
[TaxType] => NONE
[TaxAmount] => 0.00
[LineAmount] => 650.00
[AccountCode] => 200
[Quantity] => 1.0000
[LineItemID] => c413f22a-d1ff-460f-91e3-a4793eb7b963
)
[1] => SimpleXMLElement Object
(
[ItemCode] => PR1113
[Description] => Acme Doodles
[UnitAmount] => 645.00
[TaxType] => NONE
[TaxAmount] => 0.00
[LineAmount] => 645.00
[AccountCode] => 200
[Quantity] => 1.0000
[LineItemID] => c624c314-d8d8-4e08-861b-eda40af4ae5b
)
)
)
[SubTotal] => 1295.00
[TotalTax] => 0.00
[Total] => 1295.00
[UpdatedDateUTC] => 2017-03-05T22:06:10.263
[CurrencyCode] => AUD
[FullyPaidOnDate] => 2017-03-06T00:00:00
[Type] => ACCREC
[InvoiceID] => bb22c090-45b6-4357-82d0-f39daf45fa21
[InvoiceNumber] => IV1952
[Payments] => SimpleXMLElement Object
(
[Payment] => SimpleXMLElement Object
(
[PaymentID] => 8c0f5409-0fe4-4a42-85f7-056ec2e2f824
[Date] => 2017-03-06T00:00:00
[Amount] => 1295.00
[Reference] => kged;oidgs
[CurrencyRate] => 1.000000
)
)
[AmountDue] => 0.00
[AmountPaid] => 1295.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
[3] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => e0413ee8-7b41-40e9-99c4-dd6b47c65564
[ContactNumber] => CT1686
[Name] => Acme Corp Inc
)
[Date] => 2017-03-15T00:00:00
[DueDate] => 2017-03-29T00:00:00
[BrandingThemeID] => 2ced98b8-3be9-42c4-ae79-fe3c8bca3490
[Status] => AUTHORISED
[LineAmountTypes] => Exclusive
[LineItems] => SimpleXMLElement Object
(
[LineItem] => SimpleXMLElement Object
(
[ItemCode] => Support-M
[Description] => Desktop/network support via email & phone.
[UnitAmount] => 500.00
[TaxType] => OUTPUT
[TaxAmount] => 50.00
[LineAmount] => 500.00
[AccountCode] => 200
[Quantity] => 1.0000
[LineItemID] => 46963aaf-cfb4-4825-aefc-98fdb720bb60
)
)
[SubTotal] => 500.00
[TotalTax] => 50.00
[Total] => 550.00
[UpdatedDateUTC] => 2017-03-14T22:00:23.857
[CurrencyCode] => AUD
[Type] => ACCREC
[InvoiceID] => f9d3fa27-4ce6-44a3-be62-afb9baced1a5
[InvoiceNumber] => IV1947
[AmountDue] => 550.00
[AmountPaid] => 0.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
[4] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => e0413ee8-7b41-40e9-99c4-dd6b47c65564
[ContactNumber] => CT1686
[Name] => Acme Corp Inc
)
[Date] => 2017-03-15T00:00:00
[DueDate] => 2017-03-29T00:00:00
[BrandingThemeID] => 2ced98b8-3be9-42c4-ae79-fe3c8bca3490
[Status] => AUTHORISED
[LineAmountTypes] => Exclusive
[LineItems] => SimpleXMLElement Object
(
[LineItem] => Array
(
[0] => SimpleXMLElement Object
(
[ItemCode] => Support-M
[Description] => Desktop/network support via email & phone.
[UnitAmount] => 500.00
[TaxType] => OUTPUT
[TaxAmount] => 150.00
[LineAmount] => 1500.00
[AccountCode] => 200
[Quantity] => 3.0000
[LineItemID] => a6464c91-f628-4060-8692-268bcd7a2d3d
)
[1] => SimpleXMLElement Object
(
[ItemCode] => DevD
[Description] => Development work - developer onsite per day
[UnitAmount] => 650.00
[TaxType] => OUTPUT
[TaxAmount] => 325.00
[LineAmount] => 3250.00
[AccountCode] => 200
[Quantity] => 5.0000
[LineItemID] => 6ae30639-433f-40c9-8505-65f60f1fa0ff
)
)
)
[SubTotal] => 4750.00
[TotalTax] => 475.00
[Total] => 5225.00
[UpdatedDateUTC] => 2017-03-14T22:01:23.03
[CurrencyCode] => AUD
[Type] => ACCREC
[InvoiceID] => f00039cd-ad9f-403d-becc-d5508cca82ce
[InvoiceNumber] => IV1948
[AmountDue] => 5225.00
[AmountPaid] => 0.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
[5] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => 233265a4-d4e8-4bee-8110-1c5bb483283d
[ContactNumber] => CT1598
[Name] => JPV Gutters And Remodeling
)
[Date] => 2017-04-04T00:00:00
[DueDate] => 2017-04-18T00:00:00
[Status] => AUTHORISED
[LineAmountTypes] => Exclusive
[LineItems] => SimpleXMLElement Object
(
[LineItem] => Array
(
[0] => SimpleXMLElement Object
(
[ItemCode] => TSL - Black
[Description] => Company Branded T-Shirt Large Black. Check out our website for other offers!
[UnitAmount] => 40.00
[TaxType] => OUTPUT
[TaxAmount] => 16.00
[LineAmount] => 160.00
[AccountCode] => 200
[Quantity] => 4.0000
[LineItemID] => 030bf68b-f75e-4280-b85f-c0de0946aeb9
)
[1] => SimpleXMLElement Object
(
[ItemCode] => TSS - Black
[Description] => Company Branded T-Shirt Small Black. Check out our website for other offers!
[UnitAmount] => 40.00
[TaxType] => OUTPUT
[TaxAmount] => 24.00
[LineAmount] => 240.00
[AccountCode] => 200
[Quantity] => 6.0000
[LineItemID] => 60783d08-a2dd-414a-b809-41a62e06b8d9
)
)
)
[SubTotal] => 400.00
[TotalTax] => 40.00
[Total] => 440.00
[UpdatedDateUTC] => 2017-04-03T22:58:04.85
[CurrencyCode] => AUD
[Type] => ACCREC
[InvoiceID] => 847a7ffa-7dfe-4de3-89d4-858731097875
[InvoiceNumber] => IV2023
[AmountDue] => 440.00
[AmountPaid] => 0.00
[AmountCredited] => 0.00
[SentToContact] => false
[CurrencyRate] => 1.000000
[HasAttachments] => false
)
)
)
)
我现在需要遍历每个发票并从每张发票中提取一些数据。我可以使用以下方式计算发票总数:
$totalInvoices = count($invoices->Invoices[0]);
返回6.我然后尝试使用foreach循环从每张发票中获取[InvoiceNumber]值,如下所示:
$recnum = 0;
foreach($invoices as $invoice){
$invoiceNumber = $invoices->Invoices[0]->Invoice[$recnum]->InvoiceNumber;
$recnum++;
}
但它只返回前5个InvoiceNumbers而不是第6个/最后一个(IV2023)。我之前没有使用过这种类型的数据,所以我可能会在这里遗漏一些东西,但它看起来像普通的PHP数组。
更新:我已按如下方式更新了循环:
// Loop through each invoice
$recnum = 0;
foreach($invoices as $invoice){
$invoiceNumber = $invoices->Invoices[0]->Invoice[$recnum]->InvoiceNumber;
echo '$invoiceNumber for $recnum '.$recnum.' is: '.$invoiceNumber.'<br>';
pr($invoice);
echo '<hr>';
/*
if (isset($_SESSION['DebugMode']) && $_SESSION['DebugMode'] == 'TRUE') {
pr($invoice);
}
*/
$recnum++;
}
这是在浏览器中输出以下内容(太大而无法包含在此处):
https://gist.github.com/databuzz/f00d97f7bc0096fc7253fa6489c06845
最近2张发票和解析数组似乎有问题?
答案 0 :(得分:0)
试试这个foreach
循环,希望它能正常提取发票信息。
总数没有。发票:
$numberOfInvoices=count($invoiceObject->Invoices->Invoice);
提取发票信息:
foreach($invoiceObject->Invoices->Invoice as $invoiceObj)
{
echo "Invoice Number: ".$invoiceObj->InvoiceNumber;
echo "Invoice Status: ".$invoiceObj->Status;
}
答案 1 :(得分:0)
我认为,这是实现目标的最简单方法:
$invoiceNumbers = array();
foreach($invoices->Invoices->Invoices as $invoice){
$invoiceNumbers[] = $invoice->InvoiceNumber;
}
echo "<pre>";
print_r($invoiceNumbers);