note 26306 added to function.sort
| From: | whit at transpect dot com | Date: | Fri, 25 Oct 2002 19:06:17 +0000 |
| Subject: | note 26306 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-38564@lists.php.net to get a copy of this message | ||
fmmarzoa's qsort_multiarray gets the last record with the removal of a "-1" from the
3rd line of the function definition. Here's a routine using it that takes a tab-delimited table
with the column names in the first row, and displays it from an arbitrary column on the left to the
last on the right with option to sort by any one column:
<?php
//tab-delimited ASCII file to sort - no more than one linefeed at end
$infile="members.txt";
//first column on left to display - count from 0
$firstcol="1";
function qsort_multiarray($array, $num = 0, $order = "ASC", $left = 0, $right = -1) {
if($right == -1) {
$right = count($array);
}
$links = $left;
$rechts = $right;
$mitte = $array[($left + $right) / 2][$num];
if($rechts > $links) {
do {
if($order == "ASC") {
while($array[$links][$num]<$mitte) $links++;
while($array[$rechts][$num]>$mitte) $rechts--;
} else {
while($array[$links][$num]>$mitte) $links++;
while($array[$rechts][$num]<$mitte) $rechts--;
}
if($links <= $rechts) {
$tmp = $array[$links];
$array[$links++] = $array[$rechts];
$array[$rechts--] = $tmp;
}
} while($links <= $rechts);
if ($left < $rechts) $array = qsort_multiarray($array,$num,$order,$left,
$rechts);
if ($links < $right) $array = qsort_multiarray($array,$num,$order,$links,$right);
}
return $array;
}
$mem=file("$infile");
$line=explode("\t","$mem[0]");
$cnt=count($line);
echo
"<html><head><title>$infile</title></head><body>";
echo "<table border=\"1\"><tr>";
for ($i=$firstcol;$i<$cnt;$i++) {
echo "<td><a
href=\"$PHP_SELF?col=$i\">$line[$i]</a></td>";
}
echo "</tr>";
$cnt=count($mem);
for ($i=$firstcol;$i<$cnt;$i++) {
$line=explode("\t","$mem[$i]");
$cnt2=count($line);
for ($j=$firstcol;$j<$cnt2;$j++) {
$mem2[$i][$j]=$line[$j];
}
}
if(!isset($col)) $col=$firstcol;
$mem2=qsort_multiarray($mem2,$col);
for ($i=0;$i<$cnt;$i++) {
echo "<tr>";
for ($j=$firstcol;$j<$cnt2;$j++) {
$show=$mem2[$i][$j];
echo "<td>$show</td>";
}
echo "</tr>";
}
echo "</table></body></html>";
?>
--
http://www.php.net/manual/en/function.sort.php
http://master.php.net/manage/user-notes.php?action=edit+26306
http://master.php.net/manage/user-notes.php?action=delete+26306
http://master.php.net/manage/user-notes.php?action=reject+26306