【问题标题】:simplexml_load_file remove duplicate keyssimplexml_load_file 删除重复键
【发布时间】:2012-09-10 20:14:48
【问题描述】:

我正在加载一个 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-&gt;item[1]-&gt;title

【问题讨论】:

    标签: php simplexml array-unique


    【解决方案1】:

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

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

    下一步是删除重复项:

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

    注意:可能需要根据您的需要进行调整。

    【讨论】:

      【解决方案2】:

      您看过 PHP 手册的帮助部分了吗?快速搜索显示有人需要类似的东西,因此在“object_unique”函数中提供了他们的努力。

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

      这可能无法以最简洁的方式满足您的需求,但应该提供一个起点。 PHP 对象不能以您尝试的方式被视为数组。

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

      【讨论】:

      • spl_object_hash 只能告诉您两个变量是否指向完全相同的对象实例。它无法告诉您两个对象是否具有相同的内容。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-02-04
        • 2012-12-13
        • 2014-11-29
        • 2018-09-06
        • 2014-04-26
        • 2022-08-17
        • 2019-09-30
        • 2023-03-04
        • 2017-01-13
        相关资源
        最近更新 更多