我正在使用PHPs simple xml读取XML文件。 I遍历每个类别节点以显示包含的节点数据,具体取决于所选的类别,从而显示子类别。我需要检测某个类别何时没有子类别。我需要的是将所有的parent_id节点放入一个数组中,然后查看cat_id是否在数组中以检测它是否有任何依赖。
$cat_xml = simplexml_load_file(XML_PATH.'categories/'. $clientID . '.xml');
//get all the categories and go through each one
foreach($cat_xml as $cat){
if ( //WHAT GOES HERE?? ){
//display if cat is top level or has been selected
if ($cat->parentID == $cat_parent_id){
//check product isn't set to hidden
if ($cat->visible == 1){
echo '<div class="catbox">';
echo "<a href=\"index.php?pageID=$pageID&cat_parent_id=$cat->id&cat_name=$cat->Name\"> $cat->Name </a><br />";
echo '</div>';
}
}
} else {
echo '<p>There are no sub categories.</p>';
}
}
[编辑]
XML在下面,我需要获得一个“parentID”值数组
<categories> <category> <id>740073</id> <Name>Leetee Cat 1</Name> <parentID>0</parentID> <Description><![CDATA[Charlotte Balbier Weddinhg <em>dresses</em>,<strong> blah blah blah!!<br /> </strong>]]></Description> <imageURL>alice-charlotte-balbier-english-tea-party-collection-2011.jpg</imageURL> <sequence>0</sequence> <visible>1</visible> </category>
答案 0 :(得分:1)
XPath可用于搜索包含当前类别的父ID的类别。
foreach($cat_xml as $cat) {
$cat_id = intval($cat->id);
// Find categories that call this one their parent
$child_cats = $cat_xml->xpath("/categories/category[parentID/text()={$cat_id}]");
if (!empty($child_cats)){
//display if cat is top level or has been selected
if ($cat->parentID == $cat_parent_id){
// ....
}
} else {
echo '<p>There are no sub categories.</p>';
}
}