2010-08-06 43 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

只有逗号(,)丢失阵列中后,第一个“订单项”