|  | Posted by Peter J Ross on 11/25/05 01:22 
Greetings. I'm quite new to PHP and have very little knowledge ofprogramming in general, so I may be missing something obvious, but I
 can't find a solution to the following difficulty in either the PHP
 manual or Google.
 
 I have a tab-delimited text file containing numerous lines of data,
 which is searched for matches to a query from an HTML form. I want to
 be able to sort the resulting two-dimensional array of matches by one
 column only, leaving the other columns unsorted. For example, if three
 lines match "bar0",
 
 foo02  bar01  foobar  foobar
 foo03  bar02  foobar  foobar
 foo01  bar01  foobar  foobar
 
 should become
 
 foo02  bar01  foobar  foobar
 foo01  bar01  foobar  foobar
 foo03  bar02  foobar  foobar
 
 not
 
 foo01  bar01  foobar  foobar
 foo02  bar01  foobar  foobar
 foo03  bar02  foobar  foobar
 
 The following method works for sorting on the second column, but seems
 both inelegant and inflexible (especially if I want to sort on the
 third or fourth column instead:
 
 #v+
 
 <?php
 $data = file("filename.txt");
 $sortkey = 0;
 foreach($data as $line) {
 // if the line matches a regex, do this:
 $temp = explode("\t", rtrim($line,"\n"));
 $newdata[]= array($temp[1], $sortkey, $temp[0], $temp[2], $temp[3]);
 $sortkey++;
 }
 if(sizeof($newdata) > 0) {
 sort($newdata);
 foreach($newdata as $line) {
 echo $line[2]."\t".$line[0]."\t".$line[3]."\t".$line[4]."\n";
 }
 }
 ?>
 
 #v-
 
 Is there a less clumsy way of doing this kind of thing without having
 to use a "real" database? I've tried all the inbuilt array-sorting
 functions, without any success so far.
 
 PJR :-)
 --
 Nemo hibericam exspectat inquisitionem.
 
 alt.usenet.kooks award-winners and FAQ:
 <http://www.insurgent.org/~kook-faq/>
 [Back to original message] |