LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php array sort inconsistent results (https://www.linuxquestions.org/questions/programming-9/php-array-sort-inconsistent-results-430953/)

rblampain 04-02-2006 09:37 AM

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

graemef 04-02-2006 03:30 PM

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

rblampain 04-03-2006 12:34 AM

Thank you, your comment brings some clarity.


All times are GMT -5. The time now is 02:36 AM.