Created
July 26, 2014 09:34
-
-
Save phpfiddle/b1f80276a7ce7336225b to your computer and use it in GitHub Desktop.
[ Posted by Nikita Rushmanov ] SimpleXML foreach unset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$xml = " | |
<results> | |
<result> | |
<guid>a</guid> | |
<version>3</version> | |
</result> | |
<result> | |
<guid>b</guid> | |
<version>2</version> | |
</result> | |
<result> | |
<guid>a</guid> | |
<version>2</version> | |
</result> | |
<result> | |
<guid>c</guid> | |
<version>1</version> | |
</result> | |
<result> | |
<guid>a</guid> | |
<version>1</version> | |
</result> | |
<result> | |
<guid>b</guid> | |
<version>1</version> | |
</result> | |
</results> | |
"; | |
$items = new SimpleXMLElement($xml); | |
$items_iterator = $items; | |
foreach($items_iterator->xpath("result") as $i_indx=>$i_item) | |
{ | |
foreach($items->xpath("result") as $k_item) | |
{ | |
if((string)$k_item->guid == (string)$i_item->guid && | |
((string)$i_item->version > (string)$k_item->version)) | |
{ | |
//reffer to: http://stackoverflow.com/a/16062633 | |
unset($k_item[0]); | |
} | |
} | |
} | |
echo $items->asXML; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment