Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 2.24 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to loop through a SimpleXMLElement object in Php?
  2. SimpleXMLElement Object
  3. (
  4.     [products] => SimpleXMLElement Object
  5.         (
  6.             [product] => Array
  7.                 (
  8.                     [0] => SimpleXMLElement Object
  9.                         (
  10.                             [product_name] => PROD1
  11.                             [price] => 100.2
  12.                             [has_special_price] => true
  13.                             [special_price_desc] => DESC SPECIAL PRICE
  14.                             [id_product_remote] => CUSTOMID-01
  15.                             [decimals] => 0
  16.                             [category_key] => 1
  17.                             [packaging_key] => 2
  18.                             [stock_type_key] => 1
  19.                         )
  20.  
  21.                     [1] => SimpleXMLElement Object
  22.                         (
  23.                             [product_name] => PROD2
  24.                             [price] => 200.2
  25.                             [has_special_price] => false
  26.                             [special_price_desc] => DEsdSC SPECIAL PRICE
  27.                             [id_product_remote] => CUSTOMID-02
  28.                             [decimals] => 0
  29.                             [category_key] => 1
  30.                             [packaging_key] => 2
  31.                             [stock_type_key] => 1
  32.                         )
  33.  
  34.                 )
  35.  
  36.         )
  37.  
  38.     [products_client] => SimpleXMLElement Object
  39.         (
  40.             [product_client] => Array
  41.                 (
  42.                     [0] => SimpleXMLElement Object
  43.                         (
  44.                             [id_client_remote] => CUSTOMCLIENT-01
  45.                             [id_product_remote] => CUSTOMID-01
  46.                             [custom_price] => 0
  47.                         )
  48.  
  49.                     [1] => SimpleXMLElement Object
  50.                         (
  51.                             [id_client_remote] => CUSTOMCLIENT-02
  52.                             [id_product_remote] => CUSTOMID-02
  53.                             [custom_price] => 1
  54.                         )
  55.  
  56.                 )
  57.  
  58.         )
  59.  
  60. )
  61.        
  62. foreach($xml_object->products->product as $key => $product){
  63.     echo $product->product_name;
  64. }
  65.        
  66. foreach($yourVar->products->product as $key => $val) {
  67.    echo $val->product_name; /// and so on
  68. }