Re: GLOB sort order

From: Date: Tue, 22 Nov 2005 11:55:46 +0000
Subject: Re: GLOB sort order
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-226259@lists.php.net to get a copy of this message
Georgi Ivanov wrote:
On Tuesday 22 November 2005 11:41, Mark Lucas wrote:
Can anyone help me please? I'm not an advanced PHP user! I'm looing for a way to sort the array returned by the GLOB function. I would like to be able to sort the result by filemtime. Any good ideas?
I think you can walk the array returned by glob and create a hash : $arr=glob(..); foreach($arr as $file){
// you can also write this like so, sometimes useful for keeping code compact and minimizing variable usage: foreach(glob(/* bla bla */) as $file) {
$stat=stat($file); $mtime=$stat[mtime];
// don't forget to use quotes!: $mtime=$stat['mtime'];
$new[$file]=$mtime; } Now you should have array like this : foo.txt => 19879823 foo1.txt => 1987987998 Now you can use array_sort function to sort the new array.
array_multi_sort() is a slightly different way of doing the same thing, sometimes handy when you need to be able to switch between different sort orders: $mFilesFound = glob(/* bla bla */); foreach ($mFilesFound as $file) { $mTimes[] = filemtime($file); } array_multisort($mTimes, SORT_DESC, $mFilesFound);
Good luck.
Thanks, Mark ___________________________________________________________________________ __
     St John's Church, Polegate - Making Disciples of Jesus Christ
___________________________________________________________________________ __ This email is intended solely for the addressee and is strictly confidential. If you are not the addressee please delete the message from your computer.


« previous php.general (#226259) next »