Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7669745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:39:54+00:00 2026-05-31T15:39:54+00:00

I’m trying to loop over the browsenodes array the Amazon Product API returns for

  • 0

I’m trying to loop over the browsenodes array the Amazon Product API returns for a requested product with ItemLookup but I can not figure out how to do it reliably since the returned array has always a different amount of levels.

I have looked for other answers here that suggest the SimpleXML iterator for example, but that doesn’t work and throws an exception because it could not be parsed as xml…

The array that is returned by amazon looks like this:

[BrowseNodes] => SimpleXMLElement Object
    (
        [BrowseNode] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 172659
                        [Name] => Televisions
                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1266092011
                                        [Name] => Television & Video
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 493964
                                                        [Name] => Categories
                                                        [IsCategoryRoot] => 1
                                                        [Ancestors] => SimpleXMLElement Object
                                                            (
                                                                [BrowseNode] => SimpleXMLElement Object
                                                                    (
                                                                        [BrowseNodeId] => 172282
                                                                        [Name] => Electronics
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

Or this for example:

[BrowseNodes] => SimpleXMLElement Object
                    (
                        [BrowseNode] => SimpleXMLElement Object
                            (
                                [BrowseNodeId] => 3764321
                                [Name] => Chin Strips
                                [Ancestors] => SimpleXMLElement Object
                                    (
                                        [BrowseNode] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 702368011
                                                [Name] => Snore Reducing Aids
                                                [Ancestors] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNode] => SimpleXMLElement Object
                                                            (
                                                                [BrowseNodeId] => 3764271
                                                                [Name] => Sleep & Snoring
                                                                [Ancestors] => SimpleXMLElement Object
                                                                    (
                                                                        [BrowseNode] => SimpleXMLElement Object
                                                                            (
                                                                                [BrowseNodeId] => 3760941
                                                                                [Name] => Health Care
                                                                                [Ancestors] => SimpleXMLElement Object
                                                                                    (
                                                                                        [BrowseNode] => SimpleXMLElement Object
                                                                                            (
                                                                                                [BrowseNodeId] => 3760931
                                                                                                [Name] => Products
                                                                                                [IsCategoryRoot] => 1
                                                                                                [Ancestors] => SimpleXMLElement Object
                                                                                                    (
                                                                                                        [BrowseNode] => SimpleXMLElement Object
                                                                                                            (
                                                                                                                [BrowseNodeId] => 3760901
                                                                                                                [Name] => Health & Personal Care
                                                                                                            )

                                                                                                    )

                                                                                            )

                                                                                    )

                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

I somehow need to reliably loop through all items in the first level of the array that contain the category and subcategory info.

if the array has more levels [1], [2] etc, those would need to be skipped.

The info I need always sits in the first level->name and then the last level->name where iscategoryroot => 1

But since the depth is always different I simply can not figure out how to do it 🙁

What can I do in this situation? How would I go to loop over arrays like this?

Thanks ! 🙂


EDIT:


So I connect to the api with curl, and build a simplexml object from it:

/* include stuff */
include("amazon_api_class.php");
include("functions.php");

// create new amazon object
$obj = new AmazonProductAPI();

try 
    {
        $result = $obj->getItemByAsin("B004NPND20,B004VRJ3E2,B000EM91FY,B00004Z2HD,B00004Z2HR,B00004Z2HZ,B00002ND60,B000N54BDO,B000BQKBCK,B0000225IO");


    } catch(Exception $e) 
    {
        echo $e->getMessage();
    }


// loop over the returned object and extract the details we need
foreach($result->Items->Item as $key => $item){

Then the complete output of the browse nodes array looks like this (always different though)

[BrowseNodes] => SimpleXMLElement Object
    (
        [BrowseNode] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 172659
                        [Name] => Televisions
                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1266092011
                                        [Name] => Television & Video
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 493964
                                                        [Name] => Categories
                                                        [IsCategoryRoot] => 1
                                                        [Ancestors] => SimpleXMLElement Object
                                                            (
                                                                [BrowseNode] => SimpleXMLElement Object
                                                                    (
                                                                        [BrowseNodeId] => 172282
                                                                        [Name] => Electronics
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 13900851
                        [Name] => Electronics Features
                        [Children] => SimpleXMLElement Object
                            (
                                [BrowseNode] => Array
                                    (
                                        [0] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 51549011
                                                [Name] => Featured Categories
                                            )

                                        [1] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 3302711
                                                [Name] => Gold Merchants
                                            )

                                        [2] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 2354568011
                                                [Name] => Specialty Boutique
                                            )

                                    )

                            )

                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 172282
                                        [Name] => Electronics
                                    )

                            )

                    )

                [2] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 1288264011
                        [Name] => All product
                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1267878011
                                        [Name] => Products
                                        [IsCategoryRoot] => 1
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 1267877011
                                                    )

                                            )

                                    )

                            )

                    )

                [3] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 3545110011
                        [Name] => Home Entertainment
                        [Children] => SimpleXMLElement Object
                            (
                                [BrowseNode] => Array
                                    (
                                        [0] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 3547652011
                                                [Name] => Blu-Ray Players
                                            )

                                        [1] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 3547650011
                                                [Name] => DVD Players
                                            )

                                        [2] => SimpleXMLElement Object
                                            (
                                                [BrowseNodeId] => 1294446011
                                                [Name] => Televisions
                                            )

                                    )

                            )

                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1294447011
                                        [Name] => Electronics
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 1267878011
                                                        [Name] => Products
                                                        [IsCategoryRoot] => 1
                                                        [Ancestors] => SimpleXMLElement Object
                                                            (
                                                                [BrowseNode] => SimpleXMLElement Object
                                                                    (
                                                                        [BrowseNodeId] => 1267877011
                                                                    )

                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

                [4] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 1293318011
                        [Name] => TVs
                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1267879011
                                        [Name] => Special Features
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 1267877011
                                                    )

                                            )

                                    )

                            )

                    )

                [5] => SimpleXMLElement Object
                    (
                        [BrowseNodeId] => 1293319011
                        [Name] => Electronics
                        [Ancestors] => SimpleXMLElement Object
                            (
                                [BrowseNode] => SimpleXMLElement Object
                                    (
                                        [BrowseNodeId] => 1267879011
                                        [Name] => Special Features
                                        [Ancestors] => SimpleXMLElement Object
                                            (
                                                [BrowseNode] => SimpleXMLElement Object
                                                    (
                                                        [BrowseNodeId] => 1267877011
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

And I always only need the first array key and then the first and last sub-item in there

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:39:56+00:00Added an answer on May 31, 2026 at 3:39 pm

    I’m not sure if I got it right, but you can probably take the idea from here.

    function getCategory($node) {
        $category = array();
        $category["Category"] = (string)$node->Name;
    
        if (isset($node->Ancestors)) {
            do {
                $node = $node->Ancestors->BrowseNode;
                if (isset($node->IsCategoryRoot) && isset($node->Ancestors)) {
                    $category["Root"] = (string)$node->Ancestors->BrowseNode->Name;
                    break;
                }
            } while (isset($node->Ancestors));
        }
    
        return $category;
    }
    

    You could easily do this on a recursive function too, but I believe its more readable looping through it. (and efficient too).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.