Php – How to rearrange XML nodes in PHP with SimpleXML

phpsimplexmlxml

I'd like to rearrange elements in the file, I do not want to use another attribute for element ordering/arrangement.

Best Solution

I think it would be better to use the DOM extension for this rather than SimpleXML. SimpleXML as far as I know does not allow you to save changes back to a file easily (at least without using DOM anyway).

Once you have a reference to the DOMNode you want to move you can first remove it:

$node = $node->parentNode->removeChild($node);

Then put it before a different node:

$node = $otherNode->insertBefore($node);

This probably isn't a detailed enough answer - please can you add some more information about exactly what you are trying to achieve?