Q:

PHP - Get values from Array in a loop

I'm working with an array and would like to grab the value for a few elements in each member of the array as I loop through them. Here's what the array looks like:
[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
                    )

            )

    )

I'm trying to loop through the array as follows and grab the InvoiceID value:
// Loop through each invoice                
            $recnum = 1;

                foreach($invoices as $invoice){

                    $invoiceID = $invoice['InvoiceID'];

                $recnum++; 

                }

but when I echo the value for $invoiceID it is empty - I gather there is a syntax error in this line:
$invoiceID = $invoice['InvoiceID']

but not sure what the error is here.

A:

Suppose you are saving this data in $xml, then the array that you need to pass to foreach loop should be:
$xml->Invoices->Invoice
Again the array elements contains data in XML Object format. So cannot get the data using $invoice['InvoiceID']. The change will be $invoice->InvoiceID.
'[]' for array and '->' for object

