Solved

glob() sorting

Posted on 2011-02-26
14
577 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
  • 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
 
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 50 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now