I am not good with xml so I need some help. This is my array that I would like to use to convert to xml output.
Array
(
[invoices] => Array
(
[0] => Array
(
[client_name] => Awesome Client
[account_number] =>
[date_created] => 02/11/2016
[form_number] => 4104
[customer_po] =>
[terms_name] => Credit Card
[date_shipped] => 12/31/1969
[billing_contact_email] =>
[billing_contact_address_line_1] =>
[billing_contact_address_line_2] =>
[billing_contact_address_line_3] =>
[billing_contact_address_line_4] =>
[billing_contact_address_city] =>
[billing_contact_address_state] => British Columbia
[billing_contact_address_postal] =>
[billing_contact_address_country] => Canada
[shipping_contact_address_line_1] =>
[shipping_contact_address_line_2] =>
[shipping_contact_address_line_3] =>
[shipping_contact_address_line_4] =>
[shipping_contact_address_city] =>
[shipping_contact_address_state] => British Columbia
[shipping_contact_address_postal] =>
[shipping_contact_address_country] => Canada
[billing_contact_first_name] => another
[billing_contact_last_name] => client
[client_rep_full_name] => Rob Montebelli
[order_rep_full_name] => Mark Graham
[job_name] => 5010
[job_number] => 2598
[event_type] => Donor Gift
[due_date] => 02/11/2016
[shipping_method] =>
[currency] => CAD
[total_taxes] => 0.00
[total_subtotal] => 1,760.16
[total] => 1,760.16
[items] => Array
(
[0] => Array
(
[taxes] => Array
(
[0] => E
)
[title] => 1889-24
[quantity] => 6
[description] => Carhartt (R) Signature Utility Duffel; TBD TBD
[unit_price] => 159.32
)
[1] => Array
(
[taxes] => Array
(
[0] => E
)
[title] => 0022-56
[quantity] => 12
[description] => Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD
[unit_price] => 67.02
)
)
)
)
)
I used Hanmant's answer in How to convert array to SimpleXML but the output I am getting is:
Awesome Client02/11/20164104Credit Card12/31/1969British ColumbiaCanadaBritish ColumbiaCanadaanotherclientRob MontebelliMark Graham50102598Donor Gift02/11/2016CAD0.001,760.161,760.16<0>E6Carhartt (R) Signature Utility Duffel; TBD TBD159.32<0>E12Zoom (TM) DayTripper Sling Compu-Messenger; TBD TBD67.02
This is my code:
$xml = null;
foreach($input['invoices'] as $invoice) {
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
array_to_xml($invoice, $xml_data);
$xml = $xml_data->asXML();
}
print_r($xml);
As you can see the output loses the key and are bunched together. What am I doing wrong?
Try using the below code, you are trying to print the XML as though it where an array. You want to output the XML to the page.
$xml = null;
foreach($input['invoices'] as $invoice) {
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
array_to_xml($invoice, $xml_data);
$xml = $xml_data->asXML();
}
echo $xml;
You will need to view source of the page otherwise the browser may parse the XML as though it was HTML.