I have a function that gets filenames from a directory, and then puts them in an array. All the filenames start with a code, as an example: -0-filename.php, -1-filename.php, -2-filename.php and -10-filename.php.
After some steps, the filenames are echoed out. The process looks like this:
rsort( $archiveArray );
$amount = count( $archiveArray );
$i = 0;
while( $i <= $amount )
{
echo $archiveArray[$i];
$i++;
}
Anyways. The problem is; when I get 10 files in the directory, and try to do the echoing process above, I get the names in a wrong order. – It’s supposed to be :
-10-filename.php
-9-filename.php
-8-filename.php
…
-1-filename.php
-0-filename.php
But instead, I get
-9-filename.php
-8-filename.php
…
-10-filename.php
-1-filename.php
-0-filename.php
What’s the quickest and easiest way to fix this?
EDIT:
If it wasn’t obvious, the filenames are not always identical, even when not including the codes. The are always in this format: -number-randomtext.php, where number is always one higher than the last one, and randomtext can really be anything.
Using
rsortwithSORT_NATURALworks if you’re running PHP 5.4. If you’re not, however:…will do the same thing. Just use
natsortto do the natural order sorting, then reverse the array.Test code (PHP 5.3.3):