访问SimpleXMLElement对象的一部分 - PHP

时间:2012-04-19 11:21:59

标签: php object simplexml

我需要在下面的SimpleXMLElement对象中循环遍历items数组,但似乎无法使用$order->order->order->items访问它。我可以使用相同的格式访问递送和帐单地址,即。 $order->order->order->delivery_address并期望以相同的方式访问items数组。但是,当我print_r($order->order->order->items)

时,我得到一个空的SimpleXMLElement对象
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 个答案:

答案 0 :(得分:1)

编辑:我刚看到您在命名时出错,需要调用父级<orders>和子项<order>


SimpleXMLElement 似乎是是空的,实际上它通常已经填充但在转储时没有显示(无论谁想到这种疯狂的行为)

你能试试吗?

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

或者使用SimpleXMLElement::children()

进行尝试

答案 1 :(得分:1)

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

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

foreach($xmlObject->xpath('/order/order[2]/items') as $item)
{
   // Do something with my $item
}

答案 2 :(得分:0)

您可以使用如下所示的循环,然后您需要做的只是$ items-&gt; id

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

      }

答案 3 :(得分: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;
            }
        }
    }