?
Solved

glob() sorting

Posted on 2011-02-26
14
Medium Priority
?
596 Views
Last Modified: 2012-05-11
I am trying to sort the files in a glob("files/*.*") import ... but Im not able to sort  the result by name ... anyone know how to do this?


Folder with text files:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt
0
Comment
Question by:Hans Inge Hagen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 4

Expert Comment

by:DanDauchy
ID: 34989314
have you tried places each name in an array and then sorting the array w/ asort:

http://www.php.net/manual/en/function.asort.php
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34991976
If the names are as you have shown them and yur want them in increasing date sequence then this will do it

$arr = glob("files/*.*");
asort( $arr );
print_r( $arr );

0
 

Author Comment

by:Hans Inge Hagen
ID: 34992083
This is what I have:

$files = glob("content/$dir/*.*"); for ($i=0; $i<count($files); $i++) { $num = $files[$i];
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995628
Try this...

$files = glob("content/$dir/*.*");

asort( $files );

for ($i=0; $i<count($files); $i++) {
    $num = $files[$i];
    ..... etc
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995756
I did try that with no success.

Maybe, there are some way to sort the files serverside? .. a script or something?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995813
What order are you trying to sort them in to? The example you gave above was already sorted in date order.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995853
It is sorted as:

2011.01.25 - Tue, 25 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.28 - Fri, 28 Jan 2011.txt

I need it to be:

2011.01.28 - Fri, 28 Jan 2011.txt
2011.01.27 - Thu, 27 Jan 2011.txt
2011.01.26 - Wed, 26 Jan 2011.txt
2011.01.25 - Tue, 25 Jan 2011.txt

in the output. This is a diary where the latest text based file should be on top. 8)
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34995871
Use arsort instead of asort.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34995930
It seems that I will need to make an Array of the $files to be able to asort/arsort the list ...
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 200 total points
ID: 34996026
"It seems that I will need to make an Array of the $files to be able to asort/arsort the list ..."

glob returns an array, that's why I coded it the way I did.
0
 

Author Comment

by:Hans Inge Hagen
ID: 34996043
When I "echo $files" the result writes  "Array" .. I dont know ..
0
 

Author Closing Comment

by:Hans Inge Hagen
ID: 34996081
Thanks
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34996149
You cannot echo an array. Use print_r instead

print_r( $files );

0
 

Author Comment

by:Hans Inge Hagen
ID: 34996223
Great. The files is now sortet correctly ... the problem might be in the file_get_content("$num");

source:

$files = glob("content/$dir/*.*");

arsort($files);
print_r( $files );

for ($i=0; $i<count($files); $i++) {
$num = $files[$i];

$str= file_get_contents("$num");
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question