Sorting SimpleXml Nodes

Hello

I have a navigation file that is in xml format, and i am manipulating it using simpleXml. I would like to be able to adjust the order of the items in the file, but am having some trouble gettings to grips with how to achieve this.

Assuming the following navigation snippet:


<about>
	<label>About Our Company!</label>
	<uri>/about</uri>
	<pages>
		<about-us>
			<label>About Us</label>
			<uri>/about</uri>
                        <order>2</order>
		</about-us>
		<contact>
			<label>Contact Us</label>
			<uri>/about/contact/</uri>
                        <order>3</order>
		</contact>
		<new-section>
			<label>new-section</label>
			<uri>/about/new-section</uri>
                        <order>1</order>
		</new-section>
	</pages>
</about>		

Now i am trying to sort the children of the “About” node, so that it’s children are ordered ascending, so 1-2-3 (new-section, about-us, contact). Any pointers on how i could achieve this?

Based on there being an “order” field, i’m guessing your display code uses that value in some form. What does your display code do?

Hi, it’s a zend framework project, so the built-in helpers for the zf display the menu according to the order property. The issue i am having here is in the backend, where the xml navigation file is being edited, where the display helpers are not used.

I found a way that casts the child elements to an array, then uses usort to compare the order, it works great! Now i just need to save the sorted children back to the parent? There seems to be no way to add a SimpleXmlElement to a SimpleXmlElement - addChild only takes strings and creates elements from there.

the structure of the SimpleXMLElement is public, so you should be able to modify the $xml->pages->children array directly?

When i try to add the sorted array of child elements to the parents children property, i get a warning:

Warning: It is not yet possible to assign complex types to properties