So I have an XML file that I am trying to loop through in order, according to the attribute, "order".
Here is an example:
<page>
<talentTrees>
<tree name="Football" order="2">
<tree name="Baseball" order="0">
<tree name="Frisbee" order="1">
</talentTrees>
</page>
My goal is to loop through each "tree" using foreach, but I want to read them in order of the order attribute: Baseball, Frisbee, Football. (0,1,2).
Sorry for poor English, not my first language.
This should give you what you want:
$trees
are now sorted by the order attribute.I wrote a recursive, expanded version that will sort by any number of attributes, in order:
For future reference, here's something you can use to query nodes via XPath and sort the result via XPath as well: SimpleDOM. In this example, I sort all
<tree/>
nodes by values of theorder
attribute:If you have many elements like this
You can use foreach:
output:
For another example: https://stackoverflow.com/a/44379495/3506219
This page gives some great examples which I thin you could use
I got the solution of xml sorting using attributes in PHP.
Please visit Below URL:-
http://ewebsurf.blogspot.com/2010/12/xml-sorting-using-attributes-php.html