Re: Ordering an array
| From: | Steve Edberg | Date: | Tue, 12 Dec 2000 00:28:24 +0000 |
| Subject: | Re: Ordering an array | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-29780@lists.php.net to get a copy of this message | ||
You'll have to break up and reassemble the names yourself. Assuming the filenames will always be of the form
<alphabetic prefix><numeric string>.<extension>
and the input and outputs are both one-dimensional arrays, you could do it as follows:
function MySort($files)
{
unset($filebits);
unset($newfiles);
reset($files);
while (list(, $name) = each($files))
{
eregi('([[:alpha:]]+)([[:digit:]]+)(.*)', $name, $bits);
$filebits[$bits[2]] = array($bits[1], $bits[3]);
}
ksort($filebits);
reset($filebits);
while (list($key, $data) = each($filebits))
{
$newfiles[] = $data[0].$key.$data[1];
}
return $newfiles;
}
Usual untested, off-the-top-of-head caveats apply; I also have the feeling there's probably a more efficient/elegant way to do this...
If the file prefix or suffix (here, 'pw' and '.htm', respectively) are invariant, you could simplify and speed up the regular expression by passing the prefix and suffix as arguments to the MySort() function.
For more info on these functions, see:
http://www.php.net/manual/function.eregi.php
http://www.php.net/manual/function.ereg.php
http://www.php.net/manual/function.ksort.php
http://www.php.net/manual/function.sort.php
-steve
At 2:30 PM -0700 12/11/00, Brian Paulson wrote:
Hello, I am taking a list of file names and putting them into an array. I am using sort with returns them in the order. 0: pw1.htm 1: pw10.htm 2: pw11.htm 3: pw12.htm 4: pw13.htm 5: pw14.htm 6: pw15.htm 7: pw16.htm 8: pw18.htm 9: pw19.htm 10: pw2.htm 11: pw20.htm 12: pw3.htm 13: pw4.htm 14: pw5.htm 15: pw6.htm 16: pw7.htm 17: pw8.htm 18: pw9.htm what I would like to be able to do is order them like this. pw1.htm pw2.htm pw3.htm pw4.htm pw5.htm pw6.htm pw7.htm pw8.htm pw9.htm pw10.htm pw11.htm pw12.htm pw13.htm pw14.htm pw15.htm pw16.htm pw18.htm pw19.htm pw20.htm Any Ideas? Thank You Brian Paulson Sr. Web Developer http://www.chieftain.com bpaulson@chieftain.com 1-800-279-6397+--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg University of California, Davis | | sbedberg@ucdavis.edu Computer Consultant | | http://aesric.ucdavis.edu/ http://pgfsun.ucdavis.edu/ |+-------------------------------------- FBI Special Agent Dale Cooper ---+