Jump to content

SimpleXMLElement sorting


sahil80

Recommended Posts

Hello,

Can somebody help me sort the array returned by SimpleXMLElement

 

I am calling the xpath method of SimpleXMLElement, which returns an array. The returned results have two records in it and i have to swap them for sorting purposes. but i just cannot do it as soon as i copy the item at index 1 to index 0 the array is lost. My zero index is set to null.  I have tried so many options with it but no luck.

 

How can i sort the items here? since i cannot just try to move the indexes. It seems SimpleXMLElement returns pointers to original objects in the XMLDocument. I have even tried array_merge_recursive to generate a third array but as soon as i make changes to third array my original array is lost.

 

Please help

 

$xmlStr = file_get_contents("example.xml");
$simxml = new SimpleXMLElement($xmlStr);
$res = $simxml->xpath("/CommandList/CheckRouting/RouterList/Router/");

$objItem = $res[0]->GroupList->Group[0]->OutwardList->Outward[0];
//Line that has problem. When i assign index 1 to index 0  and reprint the results the zero index is lost
$res[0]->GroupList->Group[0]->OutwardList->Outward[0] = $obj = $res[0]->GroupList->Group[0]->OutwardList->Outward[1];

print_r($res[0]);

Link to comment
https://forums.phpfreaks.com/topic/119819-simplexmlelement-sorting/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.