LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-28-2006, 12:18 AM   #1
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Rep: Reputation: 16
Unhappy I need help sorting an array in PHP!


Hello all,

I'm stuck again with php trying to sort an array (Key => Value pairs)!

All I want to do is sort an array naturally (case INsensitive).

Yes, I have tried a million different ways to get natcasesort($arr); to work but it just wont.

Here is the code I currently have:

### Start Code ###

$d = dir($path);
$files = array();
while (false !== ($file = $d->read())) {
if ($file == '.' OR $file == '..') continue;

$arr = array();
$arr['name'] = $file;
$arr['path'] = $d->path . $file;
$arr['size'] = filesize($arr['path']);

//array_change_key_case($arr);
natcasesort($arr);
//for ($i = 0; $i < sizeof($arr); $arr[$i] = strtolower($arr[$i]), $i++);
$files[] = $arr;
//sort($files);

}

foreach($files as $file):
echo htmlentities($file['name']);
endforeach;

### End Code ###

Thanks for your help

Last edited by socceroos; 04-28-2006 at 12:49 AM.
 
Old 04-28-2006, 12:53 AM   #2
Kethinov
Member
 
Registered: Oct 2003
Location: ESU, Student Teaching 5th Grade
Distribution: Ubuntu
Posts: 118

Rep: Reputation: 15
Maybe I'm not understanding what you're trying to do, but PHP has just about every array sorting function you could ever possibly want.

Maybe if you could show me what some example data looks like, and what it should look like after it has been sorted, I can show you how to best go about doing that.
 
Old 04-28-2006, 02:02 AM   #3
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Thanks Kethinov,

I am trying to list all the files in a specified directory and get them to be sorted alphabetically, ignoring case. For example, the filename Me2.txt would come BEFORE me10.txt.

Currently the array looks like this:

Array ( [0] => Array ( [path] => <pathtofilename> [size] => <size> [name] => <filename> ) ) Array ( [0] => Array ( [path] => <pathtofilename> [size] => <size> [name] => <filename> ) [1] => Array ( [path] => <pathtofilename> [size] => <size> [name] => <filename> ) ) etc............


it is supposed to sort like the example above (the filename Me2.txt would come BEFORE me10.txt).

But it doesn't sort properly. If I had a list of Aa,aA,B2,B10,C1,C2 it sorts like this:
C2
B10
aA
Aa
B2
C1

D'OH!!1
 
Old 04-28-2006, 07:23 AM   #4
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
if i get u right there is a logical mistake in your code. while u read the directory u have to put the name of each file into an array. When youre done u can sort the array.
Right now youre sorting while still reading the directory. this cannot work.

btw file10 comes before file2 in ascii-"alphabet" as ascii cannot count.

Last edited by j-ray; 04-28-2006 at 07:25 AM.
 
Old 04-28-2006, 08:13 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
First I'd suggest that you start off with a simple array. Just try and sort an array of the file names.

Code:
$fileName = array("Aa", "aA", "B2", "B10", "C1", "C2");

natsort($fileName);
echo "<br>Natural order sorting\n";
print_r($fileName);

natcasesort ($fileName);
echo "<br>Natural order sorting without case\n";
print_r($fileName);
This may then help you to move forward:
 
Old 04-28-2006, 08:16 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by j-ray
btw file10 comes before file2 in ascii-"alphabet" as ascii cannot count.
But the php natural sort algorithm tries to take that into account. See Natural sorting
 
Old 04-28-2006, 09:15 AM   #7
j-ray
Senior Member
 
Registered: Jan 2002
Location: germany
Distribution: ubuntu, mint, suse
Posts: 1,591

Rep: Reputation: 145Reputation: 145
But the php natural sort algorithm tries to take that into account. See Natural sorting

didnt know that - thanks
 
Old 04-30-2006, 08:19 PM   #8
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
J-Ray:

Even if I put the natcasesort function after the array has finished reading, it doesn't make a difference to the result.

Graemef:

The array sorting test code works as it should. But I am using $key => $value pairs in my array. Should this make any difference in how I approach sorting the array?

For example, could the natcasesort function be lost as to what to sort the array by?

Thanks for your help.
 
Old 04-30-2006, 08:32 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I'm not certain what you are holding but could you get the files sort them and then copy into another array, collecting the other information as you go?
 
Old 04-30-2006, 08:34 PM   #10
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Sorry J-Ray, I got it wrong.

After putting the natcasesort after the array finished reading, it truly randomised the results. Nothing is in any kind of order it seems.
 
Old 05-01-2006, 10:02 PM   #11
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
WOOOT! I did it!

I eventually (after much testing of how multi-dimensional arrays work) got this code:


### Start Code ###


// Get files in directory
$d = dir($path);
$files = array();
while (false !== ($file = $d->read())) {
if ($file == '.' OR $file == '..') continue;

$arr = array();
$arr['name'] = $file;
$arr['path'] = $d->path . $file;
$arr['size'] = filesize($arr['path']);

$files[] = $arr;

}


foreach ($files as $array_index => $array_data) {
$array_names[] = $array_data['name'];
}
$array_names = array_map('strtolower', $array_names);
array_multisort($array_names, SORT_ASC, $files);

foreach($files as $file):
echo htmlentities($file['name']);
endforeach;

### End Code ###

This code reads a directory and then sorts (case insensetive) the file names and prints them out in a natural order.
 
Old 05-01-2006, 10:17 PM   #12
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I must of missed something but how does it get it into natural order? I was expecting to see natsort or natcasesort somewhere?
 
Old 05-02-2006, 02:29 AM   #13
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
foreach ($files as $array_index => $array_data) {
$array_names[] = $array_data['name'];
}
$array_names = array_map('strtolower', $array_names);
array_multisort($array_names, SORT_ASC, $files);

The above lines are the key. These lines make a copy of the array, change it to lowercase, THEN sort it (array_multisort), THEN change it back to uppercase.

This is the only solution I have been able to find and CONFIRM that works with multi-dimensional arrays.
 
Old 05-02-2006, 07:49 AM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by socceroos
These lines make a copy of the array, change it to lowercase, THEN sort it (array_multisort), THEN change it back to uppercase.
But it does not put it into natural order?

Try the following, first your code, then my attempt.

PHP Code:
<?php
$path 
"./";

// Get files in directory
$d dir($path);
$files = array();
echo 
"<b>First version</b><br />";
while (
false !== ($file $d->read()))
{
   if (
$file == '.' OR $file == '..')
      continue;
   
   
$arr = array();
   
$arr['name'] = $file;
   
$arr['path'] = $path $file;
   
$arr['type'] = filetype($arr['path']);
   
$arr['size'] = filesize($arr['path']);
   
   
$files[] = $arr;
}


foreach (
$files as $array_index => $array_data) {
$array_names[] = $array_data['name'];
}
$array_names array_map('strtolower'$array_names);
array_multisort($array_namesSORT_ASC$files);

foreach(
$files as $file):
   echo 
htmlentities($file['name']) . " - " $file['path'] . "{"$file['type']. "} " $file['size']. " bytes<br />"
endforeach;


// Get files in directory
$d dir($path);
$files = array();
$farr  = array();
echo 
"<b>Second version</b><br />";
while (
false !== ($file $d->read()))
{
   if (
$file == '.' OR $file == '..')
      continue;
   
$farr[] = $file;
}

natcasesort($farr); 
foreach (
$farr as $file)
{
   
$arr = array();
   
$arr['name'] = $file;
   
$arr['path'] = $path $file;
   
$arr['type'] = filetype($arr['path']);
   
$arr['size'] = filesize($arr['path']);
   
   
$files[] = $arr;
}

foreach(
$files as $file):
   echo 
htmlentities($file['name']) . " - " $file['path'] . "{"$file['type']. "} " $file['size']. " bytes<br />"
endforeach;
?>
 
Old 05-09-2006, 02:37 AM   #15
socceroos
Member
 
Registered: Aug 2005
Location: Australia
Distribution: Ubuntu, FreeBSD, Fedora
Posts: 125

Original Poster
Rep: Reputation: 16
Sorry, no, it doesn't sort by natural order as such. But it does mean that lowercase/uppercase filenames are treated as the same.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Trouble to get Array Gerardoj Programming 5 03-28-2005 07:14 AM
php search array spoody_goon Programming 2 04-30-2004 07:20 AM
PHP array help? HappyDude Programming 1 10-13-2003 06:48 PM
[C programming] sorting strings in an array miguetoo Programming 1 05-26-2003 01:48 AM
php array gui10 Programming 2 03-26-2002 02:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration