AllOtherNamesTaken Posted June 30, 2013 Share Posted June 30, 2013 I am working on a event calendar thingy. I already can read, write, delete, etc. Only issue is, is that I want to echo them in order of coming up next, instead of the order I have saved (xml). Anyone have any suggestions? Extra Info: I loop through all the entries in the xml file to get the event. Quote Link to comment https://forums.phpfreaks.com/topic/279702-help-with-calendar/ Share on other sites More sharing options...
AllOtherNamesTaken Posted June 30, 2013 Author Share Posted June 30, 2013 Alright, here is some code: <?php $xml = simplexml_load_file("data.xml"); $array = array(); foreach ($xml->children() as $child) { $array[$child -> Name] = $child -> Date; } foreach ($array as $x => $x_value) { //Looping through array... not sure how to sort it by date } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279702-help-with-calendar/#findComment-1438601 Share on other sites More sharing options...
Solution chriscloyd Posted June 30, 2013 Solution Share Posted June 30, 2013 You have to create your own sort function, and use usort on the array $array = array(); foreach ($xml->children() as $child) { $array[] = array('Name' => $child->Name, 'Date' => $child->Date); } function sortArrayByDate($a, $b){ return strtotime($a['Date']) - strtotime($b['Date']); } usort($array, "sortArrayByDate"); Quote Link to comment https://forums.phpfreaks.com/topic/279702-help-with-calendar/#findComment-1438605 Share on other sites More sharing options...
AllOtherNamesTaken Posted June 30, 2013 Author Share Posted June 30, 2013 @chriscloyd Oh god your quote.. I seem to end up with a lot more than 122 though.. Quote Link to comment https://forums.phpfreaks.com/topic/279702-help-with-calendar/#findComment-1438607 Share on other sites More sharing options...
chriscloyd Posted June 30, 2013 Share Posted June 30, 2013 haha thanks, i was thinking just use a sort asort($array, 0); Quote Link to comment https://forums.phpfreaks.com/topic/279702-help-with-calendar/#findComment-1438608 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.