He has files with names like so:
Thursday567.htm
Wednesday345.htm
Monday123.htm
Friday789.htm
Sunday234.htm
If he reads the filenames from a directory and sorts the only by name then the will end up in this order:
Friday789.htm
Monday123.htm
Sunday234.htm
Thursday567.htm
Wednesday345.htm
The required result should be like so:
Sunday234.htm
Monday123.htm
Wednesday345.htm
Thursday567.htm
Friday789.htm
There isn't just some nice simple function or method to sort these based on the days of the week.
Here is the first version of a script I came up with for him.
CODE
<pre>
<?php
$days = array("Monday123.htm","Wednesday12.htm","Friday52.htm","Tuesday4556.htm",
"Sunday789.htm","Monday321.htm","Monday6321.htm","Wednesday654.htm",
"Friday987.htm","Tuesday741.htm","Saturday852.htm","Thursday963.htm");
$day_name = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
$day_number = array('0','1','2','3','4','5','6');
$temp_array = str_replace($day_name, $day_number, $days);
array_multisort($temp_array, SORT_ASC, SORT_STRING, $days);
echo "Output Values:\n";
var_dump($days);
?>
</pre>
Basically, the script reads the array of filenames, creates a temporary array from the first but replaces the weekday name with a numeric value instead. Then the array of names is sort in the order that the temporary array would normally be sorted in.
I'll explain in detail:
We use the <pre> tag in order to use the preformated output the the PHP parser sends to the browser. Otherwise everything would try to be on the same line or wrap around lines instead of a nice column of information.
The $days variable holds the data for the array of filenames that would be generated by reading the directory contents. This is just all of the filenames in the directory. In this case, I manually created the array since I'm not going to actually read a file directory.
The $day_name variable holds the data for the array of weekday names. This is basically our search array meaning we will later search the $days array for these names.
The $day_number variable holds the data for the array of weekday numbers. This is basically our replace array meaning we will later replace the $days array with these numbers.
$day_name and $day_number are related to each other in that the Name in $day_name must correspond to the Number in $day_number.
We create $temp_array by substituing the $day_name in $days with the corresponding $day_number!
Now we should have something like this in our $temp_array:
CODE
$days = array("1123.htm","312.htm","552.htm","24556.htm","0789.htm","1321.htm","16321.htm",
"3654.htm","5987.htm","2741.htm","6852.htm","4963.htm");
"3654.htm","5987.htm","2741.htm","6852.htm","4963.htm");
In otherwords, Sunday becomes 0, Monday becomes 1, Tuesday becomes 2, etc...
array_multisort() is a powerful function that can sort multidiminsional arrays as well as sort one array based on the data in a second array!
With this function, we will sort the $days array with the values from the $temp_array array.
So if the 4th position in $temp_array should go to the 1st position, then the 4th position in $days should also go to the 1st position!
The end result is that the files are sorted in order based on which day of the week comes first.
The var_dump() function simply flushes all of the variable information about $days to the browser in raw form.
The end result should look like this:
QUOTE
Sunday789.htm
Monday123.htm
Monday321.htm
Monday6321.htm
Tuesday741.htm
Tuesday4556.htm
Wednesday12.htm
Wednesday654.htm
Thursday963.htm
Friday52.htm
Friday987.htm
Saturday852.htm
As I said, this is the first version of the script I wrote for BuffaloHelp. He has asked that I write about it so that he can add it to the code repository he is working on for Trap17.
The second, more powerful version, I'll convert to a function an submit it to my new PHP code repository for public use. I simply don't think that code should be posted in multiple places so I will only post it on one website. This script stays here and the other one in function form with be added to Handy PHP
I won't add it right away since there are a number of other issues I need to take care of first but it will get there soon
Enjoy.


