简体   繁体   English

PHP-循环从数组获取值

[英]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: 我正在尝试如下遍历数组并获取InvoiceID值:

// 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的值时,它为空-我收集到这一行有语法错误:

$invoiceID = $invoice['InvoiceID']

but not sure what the error is here. 但不知道这里是什么错误。

First of all convert xml object into array: 首先将xml对象转换为数组:

$xml   = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);

$array = json_decode(json_encode($xml), TRUE);

$invoices = $array['Invoices']['Invoice'];

after this run loop to get invoice id: 在此运行循环后获取发票ID:

foreach($invoices as $invoice){

     $invoiceID = $invoice['InvoiceID']

}

Try this 尝试这个

  $invId = array();

                foreach($invoices['Invoices']['Invoice'] as $invoice){

                    $invId[] = $invoice['InvoiceID'];

                 }
print_r($invId);

echo $invId[0]; echo $ invId [0]; // for individual value //对于单个值

Suppose you are saving this data in $xml, then the array that you need to pass to foreach loop should be: $xml->Invoices->Invoice 假设您将此数据保存在$ xml中,则需要传递给foreach循环的数组应为:$ xml-> Invoices-> Invoice

Again the array elements contains data in XML Object format. 同样,数组元素包含XML对象格式的数据。 So cannot get the data using $invoice['InvoiceID']. 因此无法使用$ invoice ['InvoiceID']获取数据。 The change will be $invoice->InvoiceID. 更改将为$ invoice-> InvoiceID。

'[]' for array and '->' for object “ []”表示数组,“->”表示对象

@Gaurav @Gaurav

Last InvoiceID will be produced after iteration. 最后的InvoiceID将在迭代后产生。 If all InvoiceID required then little change required in your code. 如果需要所有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']
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM