Search code examples
phpsimplexmladdchild

SimpleXml PHP addchild before any child


I know how to add child nodes with SimpleXml and PHP but i have a problem with the orders of those child elements. Lets say i have this xml structure

album gallery1 gallery2 gallery2 . . . galleryN /album

This is how it is right now when I add childs with the basic code

$galleryElement = $sxe->addChild("gallery"); 

I want the order of child elements in this case to be on the opposite the last gallery that i will add to be first child of Album.

example:

album gallery3 gallery2 gallery1

/album

Any one knows some trick here


Solution

  • It looks like you want to prepend to your container. Unfortunately simplexml doesn't have this function natively, but this previous answer will assist you in getting it working

    SimpleXML how to prepend a child in a node?