【发布时间】:2014-08-19 05:36:16
【问题描述】:
我一直在想办法解决这个问题,但就是想不通。
我有这些我正在使用的 xml 对象:
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13463
[name] => A Athletic Wear
[path] => A Athletic Wear
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13475
[name] => A Accessories
[path] => A Accessories
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13482
[parent_id] => 13463
[name] => Crewneck Sweatshirts
[path] => A Athletic Wear -> Crewneck Sweatshirts
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Athletic_Wear/Crewneck_Sweatshirts/80.gif
)
)
SimpleXMLElement Object
(
[@attributes] => Array
(
[product_category_id] => 13483
[parent_id] => 13475
[name] => Aprons
[path] => A Accessories -> Aprons
[thumburl_front] => REPLACE_DOMAIN_WITH/images/publishers/440/ProductCategories/A_Accessories/Aprons/80.gif
)
)
什么是正确的循环使用,使最终的标记看起来像这样:
<ul>
<li>A Athletic Wear</li>
<ul>
<li>Crewneck Sweatshirt</li>
</ul>
<li>A Accessories</li>
<ul>
<li>Aprons</li>
</ul>
</ul>
这是我的功能:
function GetCatalogyList() {
global $url, $get_product_catalog;
$xml = simplexml_load_file($url . $get_product_catalog . "");
foreach ($xml as $items) {
$pcid = $items["product_category_id"];
$pid = $items["parent_id"];
$name = $items["name"];
// Logic to iterate through the xml so that product_category_id elements
// appears under the parent_id element using unordered list.
}
return $return;
}
如果这是 SQL,我会简单地使用 WERE 子句并让其余部分继续进行。但这是我以前没有遇到过的,需要帮助。谢谢。
【问题讨论】:
-
也许这会对你有所帮助stackoverflow.com/questions/10351713/…
-
这与 SQL 中的相同。您从 SQL 查询中获取结果,然后解析该数组。唯一的区别是该数组是如何被提取的。
-
您能否提供该 XML 文档的示例? (不是结果,即
SimpleXMLElement)