pepes Posted November 17, 2009 Share Posted November 17, 2009 Does anybody know how can i remove duplicate entries from an Simplexml Object. Here is the code: $xml = simplexml_load_string($response); foreach ($xml->Items->Item as $res){ $title = $res->ItemAttributes->Title; .. I want to remove duplicates by $title and display only the first one. Thanks an advance. Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/ Share on other sites More sharing options...
genericnumber1 Posted November 17, 2009 Share Posted November 17, 2009 You could just rely on the usual override mechanics of an array and recreate the array with unique values... <?php $xml = simplexml_load_string($response); $items = &$xml->Items->Item; $uniqueItems = array(); for($i = count($items)-1; $i >= 0; --$i) { $uniqueItems[$items[$i]->ItemAttributes->Title] = &$items[$i]; } foreach($uniqueItems as $item) { $title = $item->ItemAttibutes->Title; // ... } ?> Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-959436 Share on other sites More sharing options...
pepes Posted November 18, 2009 Author Share Posted November 18, 2009 Thank you for your quick response, genericnumber1 There is a little problem when i use the code, i get the error "Illegal offset type in" on line $uniqueItems[$items[$i]->ItemAttributes->Title] = &$items[$i]; Do you know what could be wrong? Thanks again Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-959883 Share on other sites More sharing options...
genericnumber1 Posted November 18, 2009 Share Posted November 18, 2009 What is the type of $xml->Items->Item->ItemAttributes->Title? I assumed it was just a string. If it's an array or another object, you'll need to use a different method. Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-960263 Share on other sites More sharing options...
pepes Posted November 18, 2009 Author Share Posted November 18, 2009 What is the type of $xml->Items->Item->ItemAttributes->Title? I assumed it was just a string. If it's an array or another object, you'll need to use a different method. Yes, it is a string. I thought using trim(), but then it will display just one product no matter what. The code is actually from a script that pulls products from Amazon api, but i get many duplicates with it. Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-960287 Share on other sites More sharing options...
genericnumber1 Posted November 18, 2009 Share Posted November 18, 2009 Try something like... <?php $uniqueItems[(string)$items[$i]->ItemAttributes->Title] = &$items[$i]; ?> It won't work if it's not a final node though. Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-960319 Share on other sites More sharing options...
pepes Posted November 18, 2009 Author Share Posted November 18, 2009 Try something like... <?php $uniqueItems[(string)$items[$i]->ItemAttributes->Title] = &$items[$i]; ?> It won't work if it's not a final node though. It worked Thank you so much, genericnumber1 Link to comment https://forums.phpfreaks.com/topic/181902-solved-remove-duplicates-from-simplexml-object/#findComment-960416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.