LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-02-2006, 09:37 AM   #1
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 833

Rep: Reputation: 35
php array sort inconsistent results


Casual PHP user.
The following script makes arrays of recursive directory listings then searches a table if there is a user defined directory or file name for any real directory and file found.
If there is, the file name or directory name is replaced by the name given by the user. The resulting new arrays are sorted, the files are simply listed when the directories are presented as a "radio" box for possible selection by the user.

All that is working fine except for the sorts which give inconsistent results. As you can see below, I have commented out the sort options that gave unsatisfactory results with a few words describing the results.

The most frustrating part is that to maintain consistency, I had to choose one type of sort for one array and another type of sort for the other array in order to get the same resulting order.

I do not expect anyone to spend too much time on this, but perhaps there is something obvious I missed that the experts can point out to me.

Thank you for your help.

function make_tree($directory){ #0
global $level, $trans2, $tree, $file, $l;
@$handle = opendir($directory);
$m=$l%5; # table has 5 elements ($m)
print "<ol type=" . chr(34) . $level[$m] . chr(34) . ">";
$l++;
###################################################
# replace real path with user given name in table #
###################################################
while(false !== ($file = readdir($handle))){ #2
$path=$directory ."/". $file;
foreach ($trans2 as $key => $value) { #4
$key = rtrim($key, "\x00..\x20");
if ($path==$key) { #5
$name=$value;
} #5
} #4
if(is_dir("$directory/$file") && ($file != ".." && $file != ".")){ #3
$dirs[$directory . "/" . $file] = $name; # put in array
} #3
elseif (substr($file,0,1)!==".") { #a
$fils[$file] = $name;
} #a
$name=""; # clear it (must do)
} # 2
##########################
# display ordered result #
##########################
$fils2=array();
foreach ($fils as $path => $name) {
if ($name) {
$fils2[$name]=$path;
}
else {
$fils2[$path]="";
}
}
@ksort($fils2); # result acceptable (A before a)
#@natcasesort($fils2); # doesn't work
#@asort($fils2); # doesn't work
foreach ($fils2 as $path => $name) {
?><li><?php echo(htmlspecialchars($path));
}
#@ksort($dirs); # all over the place
#@natcasesort($dirs); # result OK
@asort($dirs); # result acceptable (A before a)
foreach ($dirs as $path => $name) {
?><li><input type="radio" name="directori[]" value="<?php echo $path; ?>"><?php
echo(htmlspecialchars($name)); # can use path for testing
make_tree($path); # now enter recursion
}
print "</ol>";
$l-=1;
} #0

Last edited by rblampain; 04-02-2006 at 09:43 AM.
 
Old 04-02-2006, 03:30 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
well ksort sorts using the keys of your array, whilst asort uses the elements of the array. You need to look at how you setup the associate arrays $fils2 and $dir if you want a consistent approach to sorting
 
Old 04-03-2006, 12:34 AM   #3
rblampain
Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 7
Posts: 833

Original Poster
Rep: Reputation: 35
Thank you, your comment brings some clarity.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
MySQL results to html forms using PHP xemous Programming 3 08-15-2005 03:27 PM
PHP show query results Gerardoj Programming 1 05-11-2004 12:31 PM
How to auto check RAID array and send results? fclifton Linux - General 0 11-10-2003 12:43 PM
Wierd PHP results Andy@DP Programming 2 09-13-2003 11:55 PM
php - find files (photo*.jpg) and return results to an array omarswan Programming 2 06-20-2003 11:42 AM


All times are GMT -5. The time now is 05:22 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration