<?php
$xml = "<articles>
<article id=\"18357302\">
<articleCategories>
<articleCategory id=\"default\"/>
<articleCategory id=\"66607\"/>
</articleCategories>
</article>
</articles>";
$feed = simplexml_load_string($xml);
$items = $feed->article;
foreach ($items as $article) {
// $categorie = $article->articleCategories->articleCategory[id];
$categories = $article->articleCategories;
print_r($categories);
echo "<br>print_r indeed returns an array, but impossible to echo it using foreach!!!<br>";
foreach ($categories->id as $category) {
if ($category != "default") {
echo $category;
}
}
}
?>
不确定我做错了什么,我只想找到一种方法来删除articleCategories中默认值的部分
<articleCategory id=\"default\"/>
脚本需要忽略这部分,只需使用XML文件中的下一篇articleCategory,我宁愿避免使用正则表达式删除它
答案 0 :(得分:2)
脚本遍历articleCategories
标记。
但它需要迭代articleCategory
标记。
以下更改就足够了。
foreach ($categories->articleCategory as $category) {
if ($category["id"] != "default") {
echo $category["id"];
}
}