2010-08-06 45 views
0

我想編輯一個開源的PHP包裝器來導出一個XML。XML到PHP數組?

原文:

$new_invoice = array(
    array(
     "Type"=>"ACCREC", 
     "Contact" => array(
      "ContactID" => "[contact id]" 
     ), 
     "Date" => "2010-04-08", 
     "DueDate" => "2010-04-30", 
     "Status" => "SUBMITTED", 
     "LineAmountTypes" => "Exclusive", 
     "LineItems"=> array(
      "LineItem" => array(
       array(
        "Description" => "Just another test invoice", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ) 
     ) 
    ) 
); 

我已經添加了另一個LineItem的,所以它變成了這個樣子:

$new_invoice = array(
    array(
     "Type"=>"ACCREC", 
     "Contact" => array(
      "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C" 
     ), 
     "Date" => "2010-04-08", 
     "DueDate" => "2010-04-30", 
     "Status" => "DRAFT", 
     "LineAmountTypes" => "Exclusive", 
     "LineItems"=> array(
      "LineItem" => array(
       array(
        "Description" => "Just another test invoice", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ) 
      "LineItem" => array(
       array(
        "Description" => "Just another test invoice2", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ) 
     ) 
    ) 
); 

但我得到一個錯誤,說:「缺少右括號) 看來,所有的括號都在那裏,所以我很困惑。

回答

1

你在第一個LineItem數組之後錯過了一個逗號

另外,由於您的兩個數組共享相同的密鑰(「LineItem」),因此第二個數組將覆蓋第一個數組,但這與語法錯誤無關。

編輯:爲了解決這個問題(這裏假設類似的SimpleXML在使用中):

"LineItems"=> array(
    "LineItem" => array(
     array(
      "Description" => "Just another test invoice", 
      "Quantity" => "2.0000", 
      "UnitAmount" => "250.00", 
      "AccountCode" => "200" 
     ), 
     array(
      "Description" => "Just another test invoice2", 
      "Quantity" => "2.0000", 
      "UnitAmount" => "250.00", 
      "AccountCode" => "200" 
     ) 
    ) 
) 
0

你忘了一個逗號......第一個行項目後...

您代碼應該如下所示:

<?php 
$new_invoice = array(
    array(
     "Type"=>"ACCREC", 
     "Contact" => array(
      "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C" 
     ), 
     "Date" => "2010-04-08", 
     "DueDate" => "2010-04-30", 
     "Status" => "DRAFT", 
     "LineAmountTypes" => "Exclusive", 
     "LineItems"=> array(
      "0" => array(
       array(
        "Description" => "Just another test invoice", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ), 
      "1" => array(
       array(
        "Description" => "Just another test invoice2", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ) 
     ) 
    ) 
); 
?> 

此外,您的第二行項目正在覆蓋第一個項目。

我會推薦一個計數器,並增加它增加了每個訂單項後...

0

這是缺少逗號,但像其他人說,第二個將覆蓋第一。試試這個:

$new_invoice = array(
    array(
     "Type"=>"ACCREC", 
     "Contact" => array(
      "ContactID" => "7937FF1D-B135-4BD0-A219-4B621EA3808C" 
     ), 
     "Date" => "2010-04-08", 
     "DueDate" => "2010-04-30", 
     "Status" => "DRAFT", 
     "LineAmountTypes" => "Exclusive", 
     "LineItems"=> array(
      "LineItem" => array(
       array(
        "Description" => "Just another test invoice", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ), 
       array(
        "Description" => "Just another test invoice2", 
        "Quantity" => "2.0000", 
        "UnitAmount" => "250.00", 
        "AccountCode" => "200" 
       ) 
      ) 
     ) 
    ) 
); 
print_r($new_invoice); 
0

只有逗號(,)丟失陣列中後,第一個「訂單項」