Need help sorting several array issues/questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Need help sorting several array issues/questions

Post by Bigun »

Quite a few things that have been bothering me concerning arrays since the inception of my PHP coding, and I believe now they are going to be needed to be sorted out here so I can put best practices in production from here on.

What I am trying to accomplish with this snippet of code is to sort some files by their creation times. Here is what I have so far:

Code: Select all

if ($handle = opendir('.')) {
	$i=0;
	while (false !== ($file = readdir($handle))) {
		if (substr(strtoupper($file), -3, 3) == "PDF") {
			$fileage = filectime($file);
			$fileinformation[$i]['name'] = $file;
			$fileinformation[$i]['age'] = $fileage;
			$i+=1;
		}
	}
	closedir($handle);
	
	for ($i=0; $i<count($fileinformation); $i+=1) {
		echo "<li>" . substr($fileinformation[$i]['name'], 0, -4) . " Created on " . date("F d, Y - H:i:s", $fileinformation[$i]['age']) . "</li>\n\r";
	}
Here are my questions:

#1 - You should notice the array is in the format of:

Code: Select all

$arrayname[$index]['fieldname']
I normally use:

Code: Select all

$arrayname['fieldname'][$index]
Is there a right and wrong way of doing this? and why?

#2 - Going through arrays I heavily rely on for loops and using the count functions as my loop break. Is this not a good practice and should I be using foreach to go through arrays?

#3 - This is what all these questions boil down to:
With the code above, you can see I have an array containing PDF's from a directory listing along with each file's creation timestamp. How do I sort these by date ascending? I've tried it with usort, array_multisort, asort, and a few others I can't remember trying.

Bottom line, I'm trying to get a very firm handle on arrays and using them so I won't have to worry about this again.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need help sorting several array issues/questions

Post by AbraCadaver »

1. It depends on what you need. If you just need an array of files to sort by date then I would use a different format:

Code: Select all

$files[$name][$date]
2. Foreach is much easier. I always use it when appropriate.
3. I might do it like this:

Code: Select all

foreach(glob('./*.pdf') as $file) {
   $files[basename($file)] = filectime($file);
}
sort($files);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need help sorting several array issues/questions

Post by AbraCadaver »

Sorry, and then...

Code: Select all

foreach($files as $file => $date) {
   echo "<li>" . substr($file, 0, -4) . " Created on " . date("F d, Y - H:i:s", $date) . "</li>\n\r";
}
You could also use pathinfo() for basename and for the file without extension.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Need help sorting several array issues/questions

Post by Bigun »

Bizarre behavior....

The list is outputting the creation dates (in the wrong order), but no filenames. Changing the sort to the asort command outputs the filenames and the creation times, but still in the wrong order.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need help sorting several array issues/questions

Post by AbraCadaver »

Sorry. Use asort() or arsort().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Re: Need help sorting several array issues/questions

Post by Bigun »

Bingo!
Post Reply