0

I'm loading an XML file, which happens to have duplicate items in it. I wish to remove those, but trying so throws me an error:

Message: It is not yet possible to assign complex types to properties

The return of the xml function is off course an object, where the items are stored in an array. Those items are object again, so I guess this makes it a bit harder to check against duplicates.

I've tried fixing this with:

array_unique((array) $XMLObject);

But that doesn't seem to work.

Anyone got an idea?

Here's my 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"
    }

So it needs to fix unique strings at: $Object->item[1]->title

hakre
  • 193,403
  • 52
  • 435
  • 836
Gerard Nijboer
  • 502
  • 1
  • 7
  • 18

3 Answers3

0

You need to convert it to a pure array first (objects must be turned into an array):

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

Next step is to remove duplicates:

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

Note: it may have to be tweaked to suit your needs.

ionFish
  • 1,004
  • 1
  • 8
  • 20
0

Have you taken a look through the help section of the PHP manual? A quick search shows someone who has required something similar and thus provides their efforts in an "object_unique" function.

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

This might not do what you want in the neatest of fashions, but should provide a starting point. PHP Objects cannot be treated as arrays in the way you are attempting.

Your alternative would be to write a function to iterate over the SimpleXML object and maintain a separate array to note whether you've seen a particular item previously or not. You could do this by using the PHP function spl_object_hash if you know that there are duplicates of the full item level objects. This wouldn't work if only the "link" value was duplicated per object.

carlgarner
  • 89
  • 5
  • `spl_object_hash` can only tell you if two variables point at exactly the same object instance. It cannot tell you if two objects have the same content. – IMSoP Sep 12 '12 at 17:37
0

You need to tell PHP what you mean by "duplicate" - items 0 and 1 in that example aren't identical, they just have the same value for one of their properties. You'll need to loop over the items inspecting that property and seeing if it has a value you've already seen.

The easiest way to do this is by building a hash as you go (since array keys are unique by definition):

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

Also, note that SimpleXML objects don't always behave like "real" PHP objects, as they are actually a wrapper around non-PHP code.

IMSoP
  • 89,526
  • 13
  • 117
  • 169