Forum Moderators: coopster

Message Too Old, No Replies

PHP file sorting problem.

         

andrewsmd

12:51 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a php file called functions.php which stores all of my functions (hard to guess that one huh). Anyways, I have a function called sortArrayByDate that takes in an array of file names and sorts them by date modified with the most recent at the top. Now in all of my other PHP files I require functions.php. When I call it I get absolutely no output e.g. if I have test.php and have an array of files and call var_dump(sortArrayByDate($arrayOfFiles)); I get a syntax error. However if I paste the function of sortArrayByDate into test.php and call it then it returns the array sorted correctly no problem. Could this be a global variable problem or something. Here is my sort function code. Also, if anyone has a better way to sort an array of files by the most recent date modified I'm all ears.
//this function sorts an array by the most recently updated file it returns the array
//with the value as the filename
function sortArrayByDate($array){

$temp = $array;

$temp2 = array();

foreach($temp as $key => $i){

//set the key to the date modified with and incrementer

$modifyDate = floatval(date("YmdHis", (filemtime($i))));

//these ifs check to see how big the counter is to put in the appropriate amount of 0s
//eg. if the counter is less than 10 then we need 4 0s so the counter looks like 00002
//if it is less than 100 then we need 3 0s so the counter looks like 00055

if($key < 10){

$temp2[$i] = floatval($modifyDate."0000".$key);

}//if key < 10

elseif($key < 99){

$temp2[$i] = floatval($modifyDate."000".$key);

}//else if key < 99

elseif($key < 999){

$temp2[$i] = floatval($modifyDate."00".$key);

}//else if key < 999

elseif($key < 9999){

$temp2[$i] = floatval($modifyDate."0".$key);

}//else if key < 99
else{

$temp2[$i] = floatval($modifyDate.$key);

}//else

}//foreach

arsort($temp2);

$counter = 0;
//now we set the keys back to the values
foreach($temp2 as $key => $value){

$temp[$counter] = $key;

$counter++;

}//foreach

return $temp;

}// sortArrayByDate

andrewsmd

1:13 pm on Aug 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nevermind I found the problem. I just changed the $temp to a different variable name. I'm guessing that $temp is somewhere else, I have over 200 php files so who knows.