Re: Creating & sorting associative or 2-d array
| From: | Philip Olson | Date: | Sun, 24 Dec 2000 11:50:15 +0000 |
| Subject: | Re: Creating & sorting associative or 2-d array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31749@lists.php.net to get a copy of this message | ||
Hey Patrick,
Something like this :
$dir = '.';
$dir_handle = opendir($dir);
// Create array with last modified as value, filename as key.
// filename is to be key as it is unique, last modified is not.
while ($file_piece = readdir($dir_handle)) {
// Note: checks for file, not dirs or symlinks
if (is_file($file_piece)) {
clearstatcache();
$file_lastmod = date("Y m d", filemtime($file_piece));
$file_list[$file_piece] = $file_lastmod;
}
}
// After entire array is created, then we asort it as to
// sort alphabetically by values (last mod dates)
asort($file_list);
// Print out array in fairly raw form :-)
foreach ($file_list as $file_name => $file_lastmod) {
print "<br>$file_name => $file_lastmod";
}
closedir($dir_handle);
Regards,
Philip Olson
.sig
[ I am teachable ]
[ and will work for money : http://www.cornado.com ]
On Sun, 24 Dec 2000, Patrick Dunford wrote:
>
> I have a list of files that I want to sort by their last modified date in
> descending order. The filenames with path are stored in a simple array
> called $filelist.
>
> I figured maybe I can create an associative array or 2d array with the
> filename in one dimension and the date ( formatted as
> date("Ymd",filemtime("some_filename")) and then sort based on the dates.
> But
> I can't figure out how to create the array and sort it. All I get is lots of
> error messages.
>
> Warning: Wrong datatype in ksort() call in
> /home/trainorg/public_html/canterbury/_site/include/filestuff.php3 on line
> 139
>
> Warning: Variable passed to reset() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 11
>
> Warning: Variable passed to each() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 13
>
> Warning: Wrong datatype in ksort() call in
> /home/trainorg/public_html/canterbury/_site/include/filestuff.php3 on line
> 139
>
> Warning: Variable passed to reset() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 11
>
> Warning: Variable passed to each() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 13
>
> Warning: Wrong datatype in ksort() call in
> /home/trainorg/public_html/canterbury/_site/include/filestuff.php3 on line
> 139
>
> Warning: Variable passed to reset() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 11
>
> Warning: Variable passed to each() is not an array or object in
> /home/trainorg/public_html/canterbury/news/index.php3 on line 13
>
> =======================================================================
> Patrick Dunford, Christchurch, NZ -
> http://pdunford.godzone.net.nz/
>
> For the sake of his great name the LORD will not reject his
> people, because the LORD was pleased to make you his own.
> -- 1 Samuel 12:22
> http://www.heartlight.org/cgi-shl/todaysverse.cgi?day=20001222
> =======================================================================
> Created by Mail2Sig - ö�½>àŽ¬
> ᦞE)gdhttp://pdunford.godzone.net.nz/software/mail2sig/
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>