将PHP数组转换为XML丢失键

时间:2016-02-11 21:28:24

标签: php arrays xml simplexml

我对xml不好,所以我需要一些帮助。这是我想用来转换为xml输出的数组。

    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
                                )

                        )

                )

        )

)

我在How to convert array to SimpleXML中使用了Hanmant的答案,但我得到的输出是:

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

这是我的代码:

$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);

正如您所看到的那样,输出丢失了密钥并且聚集在一起。我做错了什么?

1 个答案:

答案 0 :(得分:0)

尝试使用下面的代码,您尝试将XML打印为数组所在的位置。您想将XML输出到页面。

$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;

您需要查看页面的来源,否则浏览器可能会像解析HTML一样解析XML。