0

How to loop through a SimpleXMLElement object? This is what I have: I tried in many ways to iterate, but no success:

SimpleXMLElement Object
(
    [products] => SimpleXMLElement Object
        (
            [product] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [product_name] => PROD1
                            [price] => 100.2
                            [has_special_price] => true
                            [special_price_desc] => DESC SPECIAL PRICE
                            [id_product_remote] => CUSTOMID-01
                            [decimals] => 0
                            [category_key] => 1
                            [packaging_key] => 2
                            [stock_type_key] => 1
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [product_name] => PROD2
                            [price] => 200.2
                            [has_special_price] => false
                            [special_price_desc] => DEsdSC SPECIAL PRICE
                            [id_product_remote] => CUSTOMID-02
                            [decimals] => 0
                            [category_key] => 1
                            [packaging_key] => 2
                            [stock_type_key] => 1
                        )

                )

        )

    [products_client] => SimpleXMLElement Object
        (
            [product_client] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [id_client_remote] => CUSTOMCLIENT-01
                            [id_product_remote] => CUSTOMID-01
                            [custom_price] => 0
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [id_client_remote] => CUSTOMCLIENT-02
                            [id_product_remote] => CUSTOMID-02
                            [custom_price] => 1
                        )

                )

        )

)

2 Answers 2

4

What have you tried?

foreach($xml_object->products->product as $key => $product){
    echo $product->product_name;
}
1
  • thanks, that is what i am looking for: I tried: productsToImport = $xmlObject->products; foreach ($producstToImport->product as $curr_product) { echo " "." c ". " "; } but didn't worked Feb 27, 2012 at 11:05
1

Try doing:


foreach($yourVar->products->product as $key => $val) {
   echo $val->product_name; /// and so on
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.