PHP 重复列表
这是我的代码,将所有类别列为列表项:
if ($xml) {
foreach($xml->children() as $IRXML) { // SHOW ONLY 5 NEWS ITEMS
foreach($IRXML->children() as $newsrelease) { // EACH NEWSRELEASE IS ONE SUBCHILD
// echo " <div class='news-image'>";
// echo "<img src='/wp-content/themes/bby/images/news-story-01.jpg' alt=''>";
// echo "</div>";
$categories = $newsrelease->Categories;
foreach($categories->children() as $category) {
if ($category != "NA") {
echo "<li class='category ".$category."'>".$category."</li> ";
}
}
}
}
}
我想做的只是显示一个类别而不重复它。
Here is my code to list all category as a list item:
if ($xml) {
foreach($xml->children() as $IRXML) { // SHOW ONLY 5 NEWS ITEMS
foreach($IRXML->children() as $newsrelease) { // EACH NEWSRELEASE IS ONE SUBCHILD
// echo " <div class='news-image'>";
// echo "<img src='/wp-content/themes/bby/images/news-story-01.jpg' alt=''>";
// echo "</div>";
$categories = $newsrelease->Categories;
foreach($categories->children() as $category) {
if ($category != "NA") {
echo "<li class='category ".$category."'>".$category."</li> ";
}
}
}
}
}
What I wanted to do is only to display a category without duplicating it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看文档中的
in_array
函数。Check out the
in_array
function from the documentation.您的意思是您只想将每个显示一次吗?如果 .children() 返回同一个孩子两次,那可能是另一个问题;如果它不是您可以更改的内容并且这是您所看到的行为,您可以保留一个类别名称/ID 数组,在显示它们时追加,并在每次循环运行时写入输出之前检查数组是否有重复项。
Do you mean you want to display each one only once? If .children() is returning the same child twice, that could be another problem; if it's not something you can change and this is the behavior you're seeing, you could keep an array of category names/ID's, append as you display them, and check the array for duplicates before writing output each time the loop runs.
假设在生成 XML 时无法消除重复项……那会更有效。
您可以在循环外部创建一个数组,然后检查是否已经看到某个类别。例子:
Assuming there is no way to eliminate the duplicates when generating the XML...that would be more efficient.
You could create an array outside of the loop, and then check to see if a category has already been seen. Example: