newb Posted July 27, 2009 Share Posted July 27, 2009 hi, how can i put these results into an array and sort by the $date variable? $dir = "/home/public_html"; $count = 0; if ($handle = opendir("$dir")) { while (($file = readdir($handle)) !== false) { $date = date("F d Y H:i:s.",filemtime("$dir/$file")); $file = str_replace('www.domain.com', '', $file); if (preg_match("/.domain.com/i",$file)) { $file = substr($file, 4); $count = $count + 1; echo "$count. <a href='http://$file'>$file</a> - $date<br />"; } } closedir($handle); echo "<br /><br />$count"; } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 Like this? <?php $files = array(); foreach (new DirectoryIterator('/some/path') as $file) { if ($file->isDir()) continue; $files[] = array( 'name' => $file->getFilename(), 'changed' => $file->getMTime(), ); } $count = count($files); usort($files, create_function('$a,$b', 'return $a["changed"] < $b["changed"];')); If you use PHP 5.3 you can replace the last line with usort($files, function ($a, $b) { return $a['changed'] < $b['changed']; }); Quote Link to comment Share on other sites More sharing options...
newb Posted July 27, 2009 Author Share Posted July 27, 2009 ah thanks again daniel, sorry about editing last thread also. Quote Link to comment Share on other sites More sharing options...
newb Posted July 27, 2009 Author Share Posted July 27, 2009 just tried this function and it doesnt output anything. not sure why..? Quote Link to comment Share on other sites More sharing options...
waynew Posted July 27, 2009 Share Posted July 27, 2009 Have you gone through the array Files and outputted? Quote Link to comment Share on other sites More sharing options...
newb Posted July 27, 2009 Author Share Posted July 27, 2009 when i add print_r($files); or echo $files it outputs Array () Quote Link to comment Share on other sites More sharing options...
newb Posted July 27, 2009 Author Share Posted July 27, 2009 ok im using this code: $dir = "/var/www/"; $count = 0; if ($handle = opendir("$dir")) { while (($file = readdir($handle)) !== false) { $date = date("F d Y H:i:s.",filemtime("$dir/$file")); $file = str_replace('www.domain.com', '', $file); if (preg_match("/.domain.com/i",$file)) { $file = substr($file, 4); $count = $count + 1; $files = array(); $files[0] = $file; $files[1] = $date; usort($files, function ($a, $b) { return $a[1] > $b[1]; }); echo "$count. <a href='http://$file'>$files[0]</a> - $files[1]<br />"; } } closedir($handle); echo "<br /><br />$count"; } but it doesnt sort the results by the date, just flips the date inplace of where the domain.com links were at.... Quote Link to comment 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.