开发者

How can I properly parse a SimpleXML Object in PHP?

I'm using the CloudFusion class to get Amazon.com data and my code is simple:

$items = $pas->item_search( "shoes", array( 
    "ResponseGroup" => "Small", 
    "SearchIndex" => "Blended" ));
$items = $items->body->Items;
echo "<pre>";
print_r( $items );
echo "</pre>";

This returns the following:

SimpleXMLElement Object (
    [Request] => SimpleXMLElement Object
        (
            [IsValid] => True
            [ItemSearchRequest] => SimpleXMLElement Object
                (
                    [Keywords] => shoes
                    [ResponseGroup] => Small
                    [SearchIndex] => Blended
               开发者_Go百科 )

        )

    [TotalResults] => 737435
    [TotalPages] => 245816
    [SearchResultsMap] => SimpleXMLElement Object
        (
            [SearchIndex] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [IndexName] => Kitchen
                    ....
        )

    [Item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [ASIN] => B0001Z95QY
                    [DetailPageURL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0001Z95QY
                    [ItemLinks] => SimpleXMLElement Object
                        (
                            [ItemLink] => Array
                                (
                                    [0] => SimpleXMLElement Object
                                        (
                                            [Description] => Technical Details
                                            [URL] => http://www.amazon.com/Household-Essentials-MS6030-Seasonal-Storage/dp/tech-data/B0001Z95QY%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB0001Z95QY
                                        ) ....................
                )

            [1] => SimpleXMLElement Object
                (
                    [ASIN] => B001ACNBZ8
                    [DetailPageURL] => http://www.amazon.com/Peet-Shoe-Dryer-Boot-Original/dp/B001ACNBZ8%3FSubscriptionId%3D0WASFFPR5B82TH4ZQB82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB001ACNBZ8
                    [ItemLinks] => SimpleXMLElement Object
                        (...................
        )

)

What I'd like to do is get down to the "Item" level, then run a foreach to get each individual entry. I tried $items = $items->Item, but this returns only the first entry.

Any ideas?


First of all, you should avoid using print_r() on SimpleXMLElement, instead just take a look at the XML using asXML(). That's also what you should post here, instead of print_r()'s output.

I can't decipher the code you have posted so I'll take a wild guess and suggest that you try something like:

foreach ($items->body->Items->Item as $Item)
{
}

At any rate, if you want to iterate over something, foreach is the answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜