PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Basically what I need to do is to sort the XML list using the 'start date' element. I've spend a good few hours googling various php code solutions but none of them seem to work. Anyone got a very simple solution that will crack this?! Help!!!!!
I think the easiest way to do this would be to parse out your XML. Then convert the 'startdate' to a unix time stamp using the php function strtotime. Then you can just sort on that unix time stamp.
Once thats done, just start re building the XML putting the elements back in after they are sorted.
I think the simplest way to do this is to convert the XML structure into a 2-dim array with keys set as the startdate field, and then use ksort on that array. After that the only thing left is to convert the array back to XML file.
<?php
$myXml = '
<training>
<course>
<startdate>2011-29-02</startdate>
<displaydate>29th February - 5th March</displaydate>
<name>CCR Normoxic</name>
<location>Malta</location>
</course>
<course>
<startdate>2011-29-04</startdate>
<displaydate>5th - 12th April</displaydate>
<name>CCR Normoxic</name>
<location>Malta</location>
</course>
<course>
<startdate>2011-29-03</startdate>
<displaydate>14th - 21st March</displaydate>
<name>Rebreather</name>
<location>Gran Canaria</location>
</course>
<course>
<startdate>2011-29-07</startdate>
<displaydate>18th - 25th July</displaydate>
<name>CCR Hypoxic</name>
<location>Malta</location>
</course>
</training>';
// Create simple xml object and print it out.
$xmlObj = simplexml_load_string($myXml);
var_dump($xmlObj);
echo '<BR>';
// Now create two arrays which we will later combine.
$dateArray = array(); // This one is of type date/time which will be the key of a new combined array.
$courseXMLstringAray = array(); // This will hold each <course> element as a string.
// Now loop through the <course> elements and populate our arrays.
foreach ($xmlObj->course as $course) {
// Get the date string.
$startDate = $course->startdate;
// Convert it to a date/time type (so we can sort it).
$date = mktime(0,0,0,substr($startDate,8,2),substr($startDate,5,2),substr($startDate,0,4));
// Add It to the new array.
array_push($dateArray, $date);
array_push($courseXMLstringAray, $course->asXML());
}
// Use the dateArray to index the array of <course> elements (in string format).
$courseXMLstringAray = array_combine($dateArray, $courseXMLstringAray);
// Now we have a new indexed array we can sort it!!
ksort($courseXMLstringAray);
// Now we can reconstruct a new XML document in the correct order.
$newXMLStr = '';
foreach ($courseXMLstringAray as $courseXMLstr) {
$newXMLStr = $newXMLStr . $courseXMLstr;
}
$newXMLStr = '<training>' . $newXMLStr . '</training>';
$xmlObj = simplexml_load_string($newXMLStr);
// Now we are back where we started but in the correct order!
var_dump($xmlObj);
?>
Oh, and by the way there is no 29th of Feb this year!
Thanks to everyone who has replied so far! I'd especially like to thank Jonesi - you are an absolute star! I just tried your code and it worked perfectly