In answering the title of your thread, the best way I've found to read arrays is using the function print_r();:
<?php $array_var = array(); // the var which stores the array ?>
<pre><?php print_r($array_var); ?></pre>
This will output the array in an easy-to-read format, where all the keys you need to access the array items are defined.
For this array:
<?php
$new_invoice = array(
array(
"Type"=>"ACCREC",
"Contact" => array(
"Name" => "Income"
),
"Date" => "2013-12-31",
"DueDate" => "2013-12-31",
"Status" => "DRAFT",
"LineAmountTypes" => "Exclusive",
"LineItems"=> array(
"LineItem" => array(
array(
"Description" => "Sales - Wet",
"Quantity" => "1.0000",
"UnitAmount" => "2500.00",
"AccountCode" => "200"
),
array(
"Description" => "Sales - Pool Table",
"Quantity" => "1.0000",
"UnitAmount" => "325.25",
"AccountCode" => "200"
)
)
)
)
);
?>
<pre><?php print_r($new_invoice); ?></pre>
The following is outputted in the HTML:
Array
(
[0] => Array
(
[Type] => ACCREC
[Contact] => Array
(
[Name] => Income
)
[Date] => 2013-12-31
[DueDate] => 2013-12-31
[Status] => DRAFT
[LineAmountTypes] => Exclusive
[LineItems] => Array
(
[LineItem] => Array
(
[0] => Array
(
[Description] => Sales - Wet
[Quantity] => 1.0000
[UnitAmount] => 2500.00
[AccountCode] => 200
)
[1] => Array
(
[Description] => Sales - Pool Table
[Quantity] => 1.0000
[UnitAmount] => 325.25
[AccountCode] => 200
)
)
)
)
)
As you can see all of your information is stored in the 0 array key:
$new_invoice[0]
Instead of:
$new_invoice
So to access your description you will need to do the following:
$new_invoice[0]['LineItems']['LineItem'][0]['Description']; // first line item
$new_invoice[0]['LineItems']['LineItem'][1]['Description']; // second line item
To add new items into your "LineItem" key, use the following code:
$new_invoice[0]['LineItems']['LineItem'][] = array( "Description" => "",
"Quantity" => "",
"UnitAmount" => "",
"AccountCode" => "");
To loop through the "LineItems" section, using a foreach like so would be plausable:
<?php
foreach($new_invoice[0]['LineItems']['LineItem'] as $lineItem) {
$lineItem['Description']; // this is the description of the line item being looped
$lineItem['Quantity']; // this is the quantity of the line item being looped
$lineItem['UnitAmount']; // so on and so forth
$lineItem['AccountCode'];
}
?>
I hope you've found this useful.