PHP Loop through Results and Parse out Values -
i'm working api returns set of invoices based on query - here's example of result returns 6 invoices store in $invoices variable:
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 , 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 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 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 ) ) ) )
i need loop through each invoice , extract data each invoice. can count total number of invoices using:
$totalinvoices = count($invoices->invoices[0]);
which returns 6. i'm trying use foreach loop grab [invoicenumber] value each invoice follows:
$recnum = 0; foreach($invoices $invoice){ $invoicenumber = $invoices->invoices[0]->invoice[$recnum]->invoicenumber; $recnum++; }
but it's returning first 5 invoicenumbers , not 6th/last 1 (iv2023). haven't worked type of data before may missing here looked liked normal php array.
update: i've updated loop follows:
// loop through each invoice $recnum = 0; foreach($invoices $invoice){ $invoicenumber = $invoices->invoices[0]->invoice[$recnum]->invoicenumber; echo '$invoicenumber $recnum '.$recnum.' is: '.$invoicenumber.'<br>'; pr($invoice); echo '<hr>'; /* if (isset($_session['debugmode']) && $_session['debugmode'] == 'true') { pr($invoice); } */ $recnum++; }
and outputting following in browser (was big include in body here):
https://gist.github.com/databuzz/f00d97f7bc0096fc7253fa6489c06845
it seems having trouble last 2 invoices , parsing array?
try foreach
loop, hope work fine in extracting invoice info.
total no. of invoices:
$numberofinvoices=count($invoiceobject->invoices->invoice);
extracting info of invoice:
foreach($invoiceobject->invoices->invoice $invoiceobj) { echo "invoice number: ".$invoiceobj->invoicenumber; echo "invoice status: ".$invoiceobj->status; }
Comments
Post a Comment