PHP 删除重复的 XML 提要条目 [英] PHP remove duplicate XML feed entries

查看:41
本文介绍了PHP 删除重复的 XML 提要条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP 解析一个 XML 文件,以创建另一个格式更好的 XML 文件,我最终将使用它来填充无序列表.

Hi i'm parsing an XML file using PHP to create another XML file in a nicer format which I am eventually going to use to populate an unordered HTML list.

但是 XML 提要有重复的条目,因此我的格式化输出也有重复的条目.我如何遍历提要并以某种方式删除重复项?如果可能,使用 PHP.我是个新手,不知道如何处理这个.

But the XML feed has duplicate entries, and thus my formatted output also has duplicate entries. How can i loop through the feed and remove the duplicates somehow? Using PHP if possible. I'm a bit of a newbie and am not sure what to do with this one.

这是一个典型的输出(我格式化的 XML 重复):

Here is a typical output (my formatted XML with duplicates):

    <films>
    <film>
    <filmtitle>Death Race 2</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=377029</filmlink>
    </film>

    <film>
    <filmtitle>Death Race 2</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=377029</filmlink>
    </film>

    <film>
    <filmtitle>Shattered Glass</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=UKIC48</filmlink
    </film>

    <film>
    <filmtitle>Shattered Glass</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=UKIC48</filmlink>
    </film>

    <film>
    <filmtitle>The Brothers Bloom</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=380196</filmlink>
    </film>

    <film>
    <filmtitle>The Brothers Bloom</filmtitle>
    <filmlink>http://www.picturebox.tv/watchnow?id=380196</filmlink>
    </film>

...and so on...

任何帮助都会很棒.谢谢.

Any help would be great. Thanks.

更新:

我在像这样循环提要之前定义了一个数组:

I have defined an array before looping through the feed like this:

$filmList = array();

在整个列表中循环时,我使用以下方法添加了条目:

When looping throughout the list I have added entries using:

array_push($filmsForList, array("filmTitle" => $title, "pictureLink" => $pictureLink);

其中 $filmTitle 和 $filmLink 是来自解析的 XML 的值.我将如何从中删除重复项?还是首先阻止他们进入?

where $filmTitle and $filmLink are the values from the parsed XML. How would I remove duplicates from that? Or stop them entering in the first place?

谢谢...

推荐答案

只需将这些对放在一个数组中,使用标题作为键,链接作为值.插入数组时,您只需覆盖重复项.

Just put those pairs in an array, use title as key, link as value. You would simply override duplicates when inserting into the array.

请参阅此问题,了解有关 Java 哈希图和 PHP 的讨论数组.

See this question for a discussion about Java hashmaps and PHP arrays.

像这样:

$a = array("one" => "one_link", "two" => "two_link", "one" => "one_link");

$target = array();

foreach ($a as $key => $value)
   $target[$key] = $value;

这会让你:

array("one" => "one_link", "two" => "two_link")

使用此设置,无需检查密钥是否已存在.

With this setup, there is no need to check if the key already exists.

这篇关于PHP 删除重复的 XML 提要条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆