如何遍历Php中的SimpleXMLElement对象?

时间:2012-02-27 10:03:13

标签: php xml simplexml

如何遍历SimpleXMLElement对象? 这就是我所拥有的: 我试过很多方法来迭代,但没有成功:

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 个答案:

答案 0 :(得分:4)

你有什么尝试?

foreach($xml_object->products->product as $key => $product){
    echo $product->product_name;
}

答案 1 :(得分:1)

尝试做:


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