2016-02-11 75 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

這不是問題! –