将 PHP 个数组转换为 XML 个丢失的键

Converting PHP arrays into XML lost keys

我不擅长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);

如您所见,输出丢失了密钥并被捆绑在一起。我做错了什么?

尝试使用下面的代码,您正在尝试打印 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;

您需要查看页面源代码,否则浏览器可能会将 XML 解析为 HTML。