Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.