0

我需要遍历items下面 SimpleXMLElement 对象中的数组,但似乎无法使用$order->order->order->items. 我可以使用相同的格式访问送货地址和帐单地址,即。$order->order->order->delivery_address并期望以相同的方式到达 items 数组。但是,当我得到一个空的 SimpleXMLElement 对象时print_r($order->order->order->items)

SimpleXMLElement Object
(
    [order] => SimpleXMLElement Object
        (
            [id] => 860268
            [shopkeeper_orderno] => 1001
            [customer] => 797476
            [creationdate] => Apr 19 2012 10:36:38:100AM
            [reference] => k2koju45rmaqfl45n20xbkmq
            [net] => 1500
            [vat] => 17.5
            [status] => 0
            [isnew] => 1
            [deductions] => 0
            [postage] => 1
            [paymentmethod] => PayPal              
            [instructions] => SimpleXMLElement Object
                (
                )

            [errors] => SimpleXMLElement Object
                (
                )

            [kashflow_synch] => 0
            [order] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [billing_address] => SimpleXMLElement Object
                                (
                                    [0] => 

                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [delivery_address] => SimpleXMLElement Object
                                (
                                    [0] => 

                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [items] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [id] => 1285158
                                            [headerID] => 860268
                                            [productID] => 4867690
                                            [description] => TEST ORDERING PF NODES - Special Offer Price
                                            [net] => 1400
                                            [vat] => 0
                                            [qty] => 1
                                            [formID] => -1
                                        )

                                    [1] => SimpleXMLElement Object
                                        (
                                            [id] => 1285159
                                            [headerID] => 860268
                                            [productID] => 4959678
                                            [description] => Wedding dress
                                            [net] => 100
                                            [vat] => 17.5
                                            [qty] => 1
                                            [formID] => -1
                                        )

                                )

                        )

                )

            [postage_tax] => 0
            [dispatched] => 0
            [paybyotherid] => -1
            [ip] => 81.168.43.121   
            [wheredidyouhearid] => -1
        )

)
4

4 回答 4

1

编辑:我刚刚看到你在命名上犯了一个错误,需要调用父<orders>项和子项<order>


SimpleXMLElement似乎是空的,实际上它通常被填充但在倾倒时不显示(谁想到了这种疯狂的行为)

你能试试这个吗?

foreach($order->orders->order as $order) { // should be orders then
  echo $item->getName();
}

或者用SimpleXMLElement::children()试试

于 2012-04-19T11:30:10.800 回答
1

您的商品实际上位于订单数组的第二个偏移量上。

我只是使用 xPath 来处理这些。

foreach($xmlObject->xpath('/order/order[2]/items') as $item)
{
   // Do something with my $item
}
于 2012-04-19T11:31:20.893 回答
0

您可以使用如下所示的循环,然后您需要做的就是 $items->id

   foreach($order->children()->children()->items as $items)
      {

      }
于 2012-04-19T11:30:20.587 回答
0

使用 Dan Lees 的建议,我尝试了 SimpleXMLElement::children() 并做了以下工作

foreach ($order->children() as $order) {
        foreach ($order->children() as $order_details) {
            foreach ($order_details->children() as $order_items) {
                echo $order_items->id;
            }
        }
    }
于 2012-04-19T13:10:41.603 回答