By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

"asort" is not doing what it is supposed to do

P: n/a
Hello everybody,

I have this problem reading the contents of a directory. here is my code:

$my_dir = dir('data/templates');

asort($my_dir);

while (false !== ($entry = $my_dir->read()))
{
echo $entry."<br>\n";
}
$my_dir->close();
But I need it to read the directory in alphabetical order and I cannot
succeed in this. "asort" is not doing what it is supposed to do. Can
somebody tell me how to achieve this?

Thank you

Françoise
Feb 13 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Françoise Debat wrote:
I have this problem reading the contents of a directory. here is my code:

$my_dir = dir('data/templates');

asort($my_dir);
Eeeerrr.... $my_dir is not an array, you cannot run asort() through it!!

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
MSN:i_*************************@hotmail.com
Jabber:iv*********@jabber.org ; iv*********@kdetalk.net
Feb 13 '07 #2

P: n/a
Iván Sánchez Ortega wrote:
Françoise Debat wrote:
>I have this problem reading the contents of a directory. here is my code:

$my_dir = dir('data/templates');

asort($my_dir);

Eeeerrr.... $my_dir is not an array, you cannot run asort() through it!!
Yes, I understand. How then can I sort the list alphabetically?

Thanks for your help

Françoise
Feb 13 '07 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Françoise Debat wrote:
>Eeeerrr.... $my_dir is not an array, you cannot run asort() through it!!

Yes, I understand. How then can I sort the list alphabetically?
First dump all the directory entries in an array, *then* sort that array.

- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

http://acm.asoc.fi.upm.es/~mr/
Proudly running Debian Linux with 2.6.17-1-686 kernel, KDE3.5.3, and PHP
5.2.0-8 generating this signature.
Uptime: 20:18:05 up 4:55, 1 user, load average: 0.56, 0.48, 1.01

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF0g81R56dWuhgxGgRAngmAKDBYZXvHH2dQNjTRo2D0S 4qDCrJwACdFAYs
Tif9X54tZaT43WaH6JjJA/s=
=ikhC
-----END PGP SIGNATURE-----
Feb 13 '07 #4

P: n/a
Message-ID: <eq**********@hercules.cohp1from Iván Sánchez Ortega
contained the following:
>Yes, I understand. How then can I sort the list alphabetically?

First dump all the directory entries in an array, *then* sort that array.
That's probably easiest. Might be more fun to do a bubble sort though
:-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 13 '07 #5

P: n/a
Geoff Berrow wrote:
Message-ID: <eq**********@hercules.cohp1from Iván Sánchez Ortega
contained the following:
>>Yes, I understand. How then can I sort the list alphabetically?
First dump all the directory entries in an array, *then* sort that array.

That's probably easiest. Might be more fun to do a bubble sort though
:-)
I'm sorry, I don't understand how to do either. Please can you point me
to an example I can learn from?

Thank you very much

Françoise
Feb 13 '07 #6

P: n/a
Message-ID: <45**********************@news.orange.frfrom Françoise
Debat contained the following:
>>
That's probably easiest. Might be more fun to do a bubble sort though
:-)

I'm sorry, I don't understand how to do either. Please can you point me
to an example I can learn from?
<?php

$path=$_SERVER['PATH_TRANSLATED'];
$dir=dirname($path);
$directory=str_replace("/","",strrchr($dir, "/"));
echo"<h2>Files in $directory</h2>\n\n";
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//print $dir . $file;
if($file!="." && $file!=".." ){
$files[]=$file;
}
}
asort($files);
closedir($dh);
}
foreach($files as $value){
echo $value."<br>";
}

?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 14 '07 #7

P: n/a
Geoff Berrow wrote:
Message-ID: <45**********************@news.orange.frfrom Françoise
Debat contained the following:
>>That's probably easiest. Might be more fun to do a bubble sort though
:-)
I'm sorry, I don't understand how to do either. Please can you point me
to an example I can learn from?

<?php

$path=$_SERVER['PATH_TRANSLATED'];
$dir=dirname($path);
$directory=str_replace("/","",strrchr($dir, "/"));
echo"<h2>Files in $directory</h2>\n\n";
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
//print $dir . $file;
if($file!="." && $file!=".." ){
$files[]=$file;
}
}
asort($files);
closedir($dh);
}
foreach($files as $value){
echo $value."<br>";
}

?>
I cannot thank you enough - thank you so much.

Regards

Françoise
Feb 14 '07 #8

P: n/a
Message-ID: <45**********************@news.orange.frfrom Françoise
Debat contained the following:
>I cannot thank you enough - thank you so much.
You are welcome. :-)

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Feb 14 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.