2016-02-11 33 views
0

我对xml不太好,所以我需要一些帮助。这是我想用来转换为xml输出的数组。将PHP数组转换为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 
           ) 

         ) 

       ) 

     ) 

) 

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

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

正如你可以看到输出丢失密钥和捆绑在一起。我究竟做错了什么?

回答

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; 

您需要查看网页源否则浏览器可以解析XML,就好像它是HTML。

+0

您也可以使用下面的代码输出xml,这应该允许您查看完整的XML而不必查看源代码。 'echo htmlentites($ xml);' –

+0

哇,谢谢你! – Blkc

+0

这不是问题! –