so I have a list of <div>
's in an xml file. I'm parsing the file using php's simpleXML
I can generate an array of all the divs with the following:
$divArray = $xmldoc->text->body->children();
But now I would like to order the $divArray by different childNodes (author, title, date) within the div.
The div looks like this.
<div>
<bibl>
<author>
<title>
<date>
</bibl>
</div>
So how can I take $divArray and sort it by <author>
or <title>
or <date>
?
Thanks for your help. jw
The basic procedure is
SimpleXMLElement
into an arraySimpleXMLElement
argumentsusort()
I can only guess at your original XML structure, but I think it looks something like this:
Step 1: Cast to array. Note that your
$divArray
is not actually an array!Step 2: write a comparison function. Since the array is a list of
SimpleXMLElement
s, the comparison function must acceptSimpleXMLElement
arguments.SimpleXMLElement
s need explicit casting to get string or integer values.Step 3: Sort the array with
usort()