PHP 重复列表

发布于 2024-11-19 03:35:42 字数 872 浏览 0 评论 0原文

这是我的代码,将所有类别列为列表项:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-11-26 03:35:42
$list_cache = array();

if ($xml)
{
    foreach($xml->children() as $IRXML) 

    foreach($IRXML->children() as $newsrelease) 
    {

        $categories = $newsrelease->Categories;

        foreach($categories->children() as $category)
        {
            if ($category != "NA")
            {
                $print_category = "<li class='category ".$category."'>".$category."</li> ";

                if( !in_array($print_category, $list_cache) )    //check if you've stored in previously in this array. In such case, in_array will return true. 
                {
                    $list_cache[] = $print_category;
                    echo $print_category;
                }
            }
        }
    }
}

查看文档中的 in_array 函数。

$list_cache = array();

if ($xml)
{
    foreach($xml->children() as $IRXML) 

    foreach($IRXML->children() as $newsrelease) 
    {

        $categories = $newsrelease->Categories;

        foreach($categories->children() as $category)
        {
            if ($category != "NA")
            {
                $print_category = "<li class='category ".$category."'>".$category."</li> ";

                if( !in_array($print_category, $list_cache) )    //check if you've stored in previously in this array. In such case, in_array will return true. 
                {
                    $list_cache[] = $print_category;
                    echo $print_category;
                }
            }
        }
    }
}

Check out the in_array function from the documentation.

娇妻 2024-11-26 03:35:42

您的意思是您只想将每个显示一次吗?如果 .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.

慢慢从新开始 2024-11-26 03:35:42

假设在生成 XML 时无法消除重复项……那会更有效。

您可以在循环外部创建一个数组,然后检查是否已经看到某个类别。例子:

$categories = $newsrelease->Categories;
$seen_categories = array();
foreach($categories->children() as $category) {
    if ($category != "NA" && !in_array($category,$seen_categories)) {
        $seen_categories[]=$category;
        echo "<li class='category ".$category."'>".$category."</li> ";
    }
} 

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:

$categories = $newsrelease->Categories;
$seen_categories = array();
foreach($categories->children() as $category) {
    if ($category != "NA" && !in_array($category,$seen_categories)) {
        $seen_categories[]=$category;
        echo "<li class='category ".$category."'>".$category."</li> ";
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文