PHP删除重复的XML提要条目

时间:2020-12-02 00:16:06

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.

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

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提要有重复的条目,因此我的格式化输出也有重复的条目。如何循环遍历提要并以某种方式删除重复的内容?如果可能的话,使用PHP。我是个新手,不知道该怎么处理这个。

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

下面是一个典型的输出(我的带重复格式的XML):

    <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.

任何帮助都是好的。谢谢。

UPDATE:

更新:

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);

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?

$filmTitle和$filmLink是解析后的XML的值。我该如何从其中删除副本?或者首先阻止他们进入?

Thanks...

谢谢……

2 个解决方案

#1


1  

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

把这些对放在数组中,用title作为键,用link作为值。您只需在插入数组时重写副本即可。

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

有关Java hashmap和PHP数组的讨论,请参见这个问题。

Edit:

编辑:

Something like this:

是这样的:

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

$target = array();

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

This will get you:

这将让你:

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

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

有了这个设置,就不需要检查键是否已经存在。

#2


5  

Try this:

试试这个:

<?php
$str=<<<'EOT'
    <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>
    </films>
EOT;

$xml=simplexml_load_string($str);

$seen=array();

$len=$xml->film->count();
for($i=0;$i<$len;$i++){
    $key=(string) $xml->film[$i]->filmlink;
    if (isset($seen[$key])) {
        unset($xml->film[$i]);
        $len--;
        $i--;
    }else{
        $seen[$key]=1;
    }
}

echo $xml->asXML();

?>

this clears duplicates by filmlink

这可以通过filmlink清除重复。

#1


1  

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

把这些对放在数组中,用title作为键,用link作为值。您只需在插入数组时重写副本即可。

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

有关Java hashmap和PHP数组的讨论,请参见这个问题。

Edit:

编辑:

Something like this:

是这样的:

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

$target = array();

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

This will get you:

这将让你:

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

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

有了这个设置,就不需要检查键是否已经存在。

#2


5  

Try this:

试试这个:

<?php
$str=<<<'EOT'
    <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>
    </films>
EOT;

$xml=simplexml_load_string($str);

$seen=array();

$len=$xml->film->count();
for($i=0;$i<$len;$i++){
    $key=(string) $xml->film[$i]->filmlink;
    if (isset($seen[$key])) {
        unset($xml->film[$i]);
        $len--;
        $i--;
    }else{
        $seen[$key]=1;
    }
}

echo $xml->asXML();

?>

this clears duplicates by filmlink

这可以通过filmlink清除重复。