2012-04-19 91 views
0

我需要循環訪問下面的SimpleXMLElement Object中的數組items,但似乎無法使用$order->order->order->items訪問它。我可以使用相同的格式訪問交付和帳單地址,即。 $order->order->order->delivery_address,並希望以相同的方式進入items數組。不過,我得到一個空的SimpleXMLElement對象,當我print_r($order->order->order->items)訪問SimpleXMLElement對象的一部分 - PHP

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 
     ) 

) 

回答

0

使用丹閱讀建議,我試過的SimpleXMLElement ::兒童(),並做了工作

foreach ($order->children() as $order) { 
     foreach ($order->children() as $order_details) { 
      foreach ($order_details->children() as $order_items) { 
       echo $order_items->id; 
      } 
     } 
    } 
1

編輯:我剛纔看到你犯了一個錯誤的命名,家長需要調用<orders>和子項<order>


SimpleXMLElement 似乎爲空,實際上它通常是滿的,但在傾銷時不顯示(誰想到這種瘋狂的行爲)

你可以試試嗎?

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

或用SimpleXMLElement::children()

+0

低於從而使父命令我不能改變的XML。我需要與我擁有的一起工作。上述給我「爲foreach()提供的無效參數」錯誤 – LeeTee 2012-04-19 12:56:50

0

嘗試,你可以使用類似下面的一個循環,那麼所有你需要做的是$物品─> ID

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

     } 
+0

試過這個,不起作用。 – LeeTee 2012-04-19 12:52:31

1

您的項目實際上是在訂單數組的第二個偏移量。

我只是使用xPath來處理這些。

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

我沒有直接訪問XML只有上面的工作。 – LeeTee 2012-04-19 12:52:12