simplexml_load_file删除重复的密钥


simplexml_load_file remove duplicate keys

我正在加载一个XML文件,该文件中恰好有重复项。我想删除这些,但这样做会给我带来一个错误:

消息:还不可能将复杂类型分配给属性

xml函数的返回是一个对象,其中的项存储在一个数组中。这些项目又是对象,所以我想这会让检查重复项变得有点困难。

我试着用来解决这个问题

array_unique((array) $XMLObject);

但这似乎并不奏效。

有人有主意吗?

这是我的xml对象:

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

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

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

下一步是删除重复:

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

注意:它可能需要调整以适应您的需求。

您看过PHP手册的帮助部分吗?快速搜索显示某人需要类似的东西,从而在"object_unique"函数中提供他们的努力。

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

这可能不会以最整洁的方式达到你想要的目的,但应该提供一个起点。PHP对象不能像您尝试的那样被视为数组。

您的替代方案是编写一个函数来迭代SimpleXML对象,并维护一个单独的数组来记录您以前是否见过特定的项。如果您知道存在完整项级别对象的重复,则可以使用PHP函数spl_object_hash来完成此操作。如果每个对象只复制"链接"值,这将不起作用。

您需要告诉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代码的包装器。