Re: PHP: Mulitiple Arrary Sort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
Vernon wrote:
I've gotten the one array down and I've figured out how to sort that array, however, I need to sort the array by the modified date of the file. Here's what I got so far:

<?php
// Open current directory
if($handle = opendir($dir)){
// Loop through all files
while(false !== ($file = readdir($handle))){
// Ignore hidden files
if(!preg_match("/^\./", $file)){
// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}

// Close directory
closedir($handle);


// if $files[] exists, sort it and print all elements in it.
if(is_array($files)){
//HERE IS MY CURRENT SORT WHICH I KNOW I NEED TO CHANGE BUT AM UNSURE HOW
sort($files);
foreach($files as $file){
$completeFileName = $dir . $file;
//I WANT TO SORT BY THIS
echo date("m-d-Y", filemtime($completeFileName)) . "<br>";
}
}

}
?>

Any ideas on how to fix this? Thanks


We need to see what is in the array, not just how you think you need to manipulate it.

Show us the basic structure of the array, with a few examples of data that would represent what you would normally be building the table out of.

Then, with that, we might be able to start helping you.


Diogo, thanks for cleaning up the code so I could realize what the OP was trying to do.

Here is an example I did a few weeks ago on how to use the array_multi_sort() function. I didn't get any feedback from the op to say if it actually fixed his problem, but it worked for me.

http://www.nabble.com/Sorting-Arrays-to19113842.html#a19118106

ok, here would be my version of this code.

<pre><?php

$dir = './';
$filepattern = '*';
$sorting_list = array();
$filemtimes = array();

# Get file/dir listing, else error message
if ( ( $list = glob( $dir . $filepattern ) ) !== false ) {

	foreach ( $list AS $file ) {

		$filemtime = filemtime( $dir . $file );

		# Build array to be sorted with filename and filemtime
		$sorting_list[] = array('filename' => $file,
					'filemtime' => $filemtime,
					);

		# This is the list of filemtimes to sort by later
		$filemtimes[] = $filemtime;

	}

	# Sort array based on $filemtimes
	# http://php.net/array-multisort     Example #3
	if ( array_multisort($filemtimes, SORT_DESC, $sorting_list) ) {
		echo "List was sorted by filemtime\n";
	} else {
		echo "List was not sorted by filemtime\n";
	}

	var_dump($sorting_list);

} else {

	echo 'Directory listing call failed!';

}

?>


Now, Diogo's version would work, except if you had two files with the same access time. One would overwrite the other in the $files_new array.

change this line:

        $files_new[filemtime( $dir . '/' . $file )] = $file;

To this:

        $files_new[$file] = filemtime( $dir . '/' . $file );


then change the ksort() call to a sort() instead.

--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux