【问题标题】:XML to PHP Array?XML 到 PHP 数组?
【发布时间】:2011-03-26 03:18:18
【问题描述】:

我正在尝试编辑开源 PHP 包装器以导出 XML。

原文:

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

但我收到一条错误消息,上面写着“期待一个右括号) 似乎所有的括号都在那里,所以我很困惑。

【问题讨论】:

    标签: php xml arrays api


    【解决方案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"
            )
        )
    )
    

    【讨论】:

      【解决方案2】:

      您在第一个行项之后忘记了逗号...

      您的代码应如下所示:

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

      您的第二个订单项也将覆盖第一个订单项。

      我会推荐一个计数器,并在添加每个 lineitem 后递增它...

      【讨论】:

        【解决方案3】:

        这是缺少的逗号,但正如其他人所说,第二个会覆盖第一个。试试这个:

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

        【讨论】:

          【解决方案4】:

          在第一个“lineitem”之后,您的数组中仅缺少逗号 (,)

          【讨论】:

            猜你喜欢
            • 2012-08-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-12-04
            • 2012-03-21
            相关资源
            最近更新 更多