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
I’m not sure if I got it right, but you can probably take the idea from here.
You could easily do this on a recursive function too, but I believe its more readable looping through it. (and efficient too).