simplexml_load_file删除重复的键

时间:2012-09-10 20:14:48

标签: php simplexml array-unique

我正在加载一个XML文件,其中包含重复的项目。 我希望删除这些,但尝试这样会让我错误:

  

消息:尚无法将复杂类型分配给属性

xml函数的返回当然是一个对象,其中的项存储在一个数组中。 那些项目再次成为对象,所以我想这使得检查重复项更加困难。

我试过用以下方法解决这个问题:

array_unique((array) $XMLObject);

但这似乎不起作用。

有人有个主意吗?

这是我的xml-object:

object(SimpleXMLElement)#19 (5) {
  ["title"]=>
  string(33) "P2000 alarmeringen Heel Nederland"
  ["link"]=>
  string(26) "http://www.p2000zhz-rr.nl/"
  ["description"]=>
  string(54) "Hier vind u alle P2000 alarmeringen van Heel Nederland"
  ["lastBuildDate"]=>
  string(31) "Mon, 10 Sep 2012 22:19:28 +0000"
  ["item"]=>
  array(300) {
    [0]=>
    object(SimpleXMLElement)#22 (5) {
      ["title"]=>
      string(4) "test"
      ["link"]=>
      string(82) "http://feedproxy.google.com/~r/p2000-nederland/~3/OeCbBLSpOKQ/p2000-nederland.html"
      ["description"]=>
      string(194) "Melding: test      Korps/Voertuig: AMBU Brabant Noord (Den Bosch-Ambu 21-102)      Capcode: 1121020<img src="http://feeds.feedburner.com/~r/p2000-nederland/~4/OeCbBLSpOKQ" height="1" width="1"/>"
      ["pubDate"]=>
      string(31) "Mon, 10 Sep 2012 22:20:08 +0000"
      ["guid"]=>
      string(25) "10-09-12_22:20.08_1121020"
    }
    [1]=>
    object(SimpleXMLElement)#23 (5) {
      ["title"]=>
      string(18) "contact supervisor"
      ["link"]=>
      string(82) "http://feedproxy.google.com/~r/p2000-nederland/~3/OeCbBLSpOKQ/p2000-nederland.html"
      ["description"]=>
      string(197) "Melding: contact supervisor      Korps/Voertuig: regio 15 Haaglanden POLITIE 10       Capcode: 1530710<img src="http://feeds.feedburner.com/~r/p2000-nederland/~4/OeCbBLSpOKQ" height="1" width="1"/>"
      ["pubDate"]=>
      string(31) "Mon, 10 Sep 2012 22:19:28 +0000"
      ["guid"]=>
      string(25) "10-09-12_22:19.28_1530710"
    }

因此需要在$Object->item[1]->title

修复唯一的字符串

3 个答案:

答案 0 :(得分:0)

首先需要将其转换为纯数组(对象必须转换为数组):

function object2array($object)
{
    return @json_decode(@json_encode($object),1);
}

下一步是删除重复项:

$array = array_unique(object2array($rawdata));

注意:可能需要调整以满足您的需求。

答案 1 :(得分:0)

您是否看过PHP手册的帮助部分?快速搜索显示需要类似内容的人,从而在“object_unique”功能中提供他们的努力。

http://www.php.net/manual/en/function.array-unique.php#108421

这可能不是你想要的最好的时尚,但应该提供一个起点。 PHP对象不能像您尝试的那样被视为数组。

您可以选择编写一个函数来迭代SimpleXML对象并维护一个单独的数组来记录您之前是否看过某个特定项目。如果您知道完整项目级别对象有重复项,则可以使用PHP函数spl_object_hash来执行此操作。如果每个对象只复制“链接”值,则无效。

答案 2 :(得分:0)

你需要告诉PHP你的意思是“重复” - 该例中的0和1项不相同,它们的属性之一只有相同的值。您需要遍历检查该属性的项目,并查看它是否具有您已经看到的值。

最简单的方法是通过构建散列(因为数组键的定义是唯一的):

$unique_items = array();
foreach ( $sx_document->item as $sx_item )
{
    // Always explicitly cast SimpleXML values to string
    $hash_key = (string)$sx_item->link;

    // This if ensures the first item with each link is kept
    // Without it, later ones would overwrite, leaving you with just the last
    if ( ! array_key_exists($hash_key, $unique_items) )
    {
        $unique_items[$hash_key] = $sx_item;
    }
}
// Throw the keys away if you want your list to be indexed 0, 1, 2, etc
$unique_items = array_values($unique_items);

另外,请注意,SimpleXML对象并不总是像“真正的”PHP对象一样,因为它们实际上是非PHP代码的包装。