_
_ Handy PHP
_
_
Tutorials, Scripts, Information And Other Resources arrow PHP Functions arrow Array Sort By Day
Friday, 21 November 2008
_

Refer A Friend using Revolution Money Exchange
Resource Menu
Handy PHP Store
Free Domain Name
PHP Functions
PHP Downloads
PHP Newsfeeds
PHP Tutorials
Webmaster Tools
User Login
TalkPHP
Get friendly help with your PHP problems for free.
www.talkphp.com
The PHP Resource Index
Handy PHP
Free PHP Help!


 
This Section Is Scheduled
For Maintenance

    The format of the PHP Functions section here at Handy PHP needs some attention soon.  Recent changes to the website have brought to light some significant layout problems with this section.  The need for a standard format for all functions has resulted in the problems.  Each function will be reformated, enhanced, archived, and updated as time allows. In the meantime, some screen resolutions may display a distorted version of the website.
Thank you for your visit and patience.

If you encounter any major problems with viewing the functions, please inform us via the forums.
If you are unable to find what you needed here, please let us know what you wanted in the forums so that we may add relevant content in the future.

Array Sort By Day Print E-mail
User Rating: / 5
PoorBest 
PHP Resources - Handy PHP Functions
Written by M. L. Griswold   
Monday, 09 October 2006
Notice, this function has been updated to correct a bug!

Description:

 
string array_sort_by_day(array)
     
 
This handy PHP function will sort an array by the order of the days in a week.  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.
Simply sorting the array alphabetically would result in the days being out of sequential order.
     
Function:
 
function array_sort_by_day($values){
// Function written by Marcus L. Griswold (vujsa)
// Can be found at http://www.handyphp.com
// Do not remove this header!
    $search_strings = array("Sunday","Sun","Su","Monday","Mon","Mo","Tuesday","Tues","Tue","Tu","Wednesday","Wed","We","Thursday","Thurs","Thur","Thu","Th","Friday","Fri","Fr","Saturday","Sat","Sa");
    $replace_string = array('0','0','0','1','1','1','2','2','2','2','3','3','3','4','4','4','4','4','5','5','5','6','6','6');
    $sort_key = array_map('ucfirst', $values);
    $sort_key = str_replace($search_strings, $replace_string, $sort_key);
    array_multisort($sort_key, SORT_ASC, SORT_STRING, $values);
    return $values;
}
   
 
Usage:
 
$days = array("Monday1123.html","Wednesday122.html","Sun332.html","Friday542.html","Tues14556.html","Sunday7819.html","Mon3221.html","Saturday8952.html","Thursday9603.html");
echo "<pre>\n";
var_dump(array_sort_by_day($days));
echo "</pre>\n";
     
Result:
 
array(9) {
[0]=>
string(11) "Sun332.html"
[1]=>
string(15) "Sunday7819.html"
[2]=>
string(15) "Monday1123.html"
[3]=>
string(12) "Mon3221.html"
[4]=>
string(14) "Tues14556.html"
[5]=>
string(17) "Wednesday122.html"
[6]=>
string(17) "Thursday9603.html"
[7]=>
string(14) "Friday542.html"
[8]=>
string(17) "Saturday8952.html"
}
     
Notes:
 
This would work for all English day names and their abbreviations.  If used for file sorting as suggested here, it would be advisable to validate the filenames prior to uploading them to ensure that the day names will be identified properly.
Function inspired by a discussion at Trap17.com.  Thanks to BuffaloHelp for the inspiration!


Discuss this article on the forums. (3 posts)
 
< Prev   Next >

_
 
_
_
© 2008 Handy PHP