我正在尝试使用ItemLookup循环遍历Amazon Product API为所请求产品返回的browsenodes数组,但我无法弄清楚如何可靠地执行它,因为返回的数组总是具有不同的级别。
我在这里寻找其他答案,例如建议使用SimpleXML迭代器,但这不起作用并引发异常,因为它无法解析为xml ...
亚马逊返回的数组如下所示:
[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
)
)
)
)
)
)
)
或者这个例如:
[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
)
)
)
)
)
)
)
)
)
)
)
)
)
)
)
我不知何故需要可靠地遍历数组第一级中包含类别和子类别信息的所有项目。
如果数组有更多级别[1],[2]等,则需要跳过这些级别。
我需要的信息始终位于第一级>名称,然后是最后一级>名称,其中iscategoryroot => 1
但由于深度总是不同,我根本无法弄清楚如何去做:(
在这种情况下我该怎么办?我将如何循环遍历这样的数组?
谢谢! :)
编辑:
所以我用curl连接到api,并从中构建一个simplexml对象:
/* 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){
然后浏览节点数组的完整输出看起来像这样(虽然总是不同)
[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
)
)
)
)
)
)
)
)
我总是只需要第一个数组键,然后是
中的第一个和最后一个子项答案 0 :(得分:5)
我不确定我是否做对了,但你可以从这里接受这个想法。
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;
}
您也可以在递归函数上轻松完成此操作,但我相信它更易于读取循环。 (而且效率也很高。)