我正在使用一个数组,并希望在循环遍历时获取数组每个成员中的一些元素的值。这是数组的样子:
[Invoices] => SimpleXMLElement Object
(
[Invoice] => Array
(
[0] => SimpleXMLElement Object
(
[Contact] => SimpleXMLElement Object
(
[ContactID] => 0e72016e-7c60-4a19-b8d0-1d2c58cc0b49
[Name] => Acme 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] => Acme 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] => Ford Traders Pty Ltd
)
[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
)
)
)
我正在尝试按如下方式遍历数组并获取InvoiceID值:
// Loop through each invoice
$recnum = 1;
foreach($invoices as $invoice){
$invoiceID = $invoice['InvoiceID'];
$recnum++;
}
但是当我回显$ invoiceID的值时它是空的 - 我收集这行中有一个语法错误:
$invoiceID = $invoice['InvoiceID']
但不确定这里的错误是什么。
答案 0 :(得分:0)
首先将xml对象转换为数组:
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), TRUE);
$invoices = $array['Invoices']['Invoice'];
在此运行循环后获取发票ID:
foreach($invoices as $invoice){
$invoiceID = $invoice['InvoiceID']
}
答案 1 :(得分:0)
试试这个
$invId = array();
foreach($invoices['Invoices']['Invoice'] as $invoice){
$invId[] = $invoice['InvoiceID'];
}
print_r($invId);
echo $ invId [0]; //用于个人价值
答案 2 :(得分:0)
假设您将此数据保存在$ xml中,那么您需要传递给foreach循环的数组应为: $ XML-> Invoices->发票
数组元素再次包含XML Object格式的数据。因此无法使用$ invoice ['InvoiceID']获取数据。更改将是$ invoice-> InvoiceID。
'[]'表示数组和' - >'对象
答案 3 :(得分:0)
@Gaurav
最后一次InvoiceID将在迭代后生成。如果需要所有InvoiceID,则代码中只需要进行少量更改。
$xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
$array = json_decode(json_encode($xml), TRUE);
$invoices = $array['Invoices']['Invoice'];
foreach($invoices as $invoice){
$invoiceID[] = $invoice['InvoiceID']
}