Reputation: 4995
I'm navgating through an XML Tree like this:
$notesXML = simplexml_load_string(XMLSTRING);
foreach($notesXML as $thenote){
$noteAttr = $thenote->attributes();
echo $noteAttr['modified'];
}
As you can see there is an attribute called "modified" as a part of the XML tree, what I want to do for now is just print out the XML tree in ascending or descending order based on the modified date. BTW the date string is formatted like this: "Tuesday 6th of September 2011 03:49:14 PM"
Thanks for any help
Upvotes: 0
Views: 1514
Reputation: 51970
You could build an array of the elements that you want to sort, then use one of the array sorting functions to re-order them.
The snippet below uses array_multisort()
to sort them by descending date order. DateTime::createFromFormat()
is used to get the Unix timestamp from the date strings.
$notes = array();
$dates = array();
foreach ($notesXML as $note) {
$notes[] = $note;
$dates[] = DateTime::createFromFormat('l jS \of F Y H:i:s A', $note['modified'])->getTimestamp();
}
array_multisort($dates, SORT_DESC, $notes);
// Loop over $notes however you like
Upvotes: 2