Forum Moderators: coopster

Message Too Old, No Replies

Sort Array Based on Number in .txt File

         

cookiemonster

3:28 pm on Jun 19, 2010 (gmt 0)

10+ Year Member



Hi,

This question might sound pretty complicated so please let me know if you need me to re-word it.
I have a list of folders in a directory which I want sorted by number of hits. I found a simple PHP hit counter which stores the number of hits in a file (hits.txt) for each folder.
So, my question is, how can I sort the list of folders by the number in each text file, from highest to lowest?

Here is my code so far - this sorts the list by date modified:


<?php
function listByDate(){
$path = glob("*",GLOB_ONLYDIR); //the double \\ is not a typo you
//have to escape it
//if the path cannot be found
if(!($path)){
echo("The specified path could not be found.");
return;
}//if

//the path is valid
else{
array_multisort(
array_map( 'filemtime', $path ),
SORT_NUMERIC,
SORT_DESC,
$path
);

foreach($path as $i){
echo("<a href='$i'>");
echo $i;
echo("</a><br>");

}//foreach
}//else
}//populateDropdown
?>


Thanks in advance, and again, sorry if this question is too complicated - feel free to ask me to re-word it.

cookiemonster

12:42 am on Jun 22, 2010 (gmt 0)

10+ Year Member



Perhaps I need to simplify my question.
I have a list of folders, and each folder contains a file called hits.txt with a number in it.
How can I sort the list of folders by the number within each hits.txt file, from greatest to least?

Thanks.
PS, I'd like to stay fairly similar to the code above.

coopster

2:58 am on Jun 22, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Loop through the directory structure and build an array of directories as key and the numeric value of hits.txt from each directory as value. Sort the array in descending order.