|
Posted by Csaba Gabor on 11/26/05 03:24
Peter J Ross wrote:
> 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.
Untested, but might try something like what I and subsequently rojaro
discussed at http://php.net/asort:
<?php
$data = file("filename.txt");
$sortCol = 1; // sort on second column
$aLines = []; // original data
$aSorted = []; // this will contain the resulting, sorted data
foreach($data as $line) {
// if the line matches a regex, do this:
$aLine[] = $line;
$temp = explode("\t", rtrim($line,"\n"));
$aSorted[] = $temp[$sortCol]; }
if ($aSorted) {
// sort on selected column retaining line associations
asort($aSorted);
// now replace selected column val with entire line
foreach ($aSorted as $idx => &$val) $val = $aLine[$idx];
// normalize the keys to be 0..sizeof($aSorted)-1
$aSorted = array_merge($aSorted);
// show the sorted lines
foreach ($aSorted as $line) echo "$line\n";
}
?>
Csaba Gabor from Vienna
Navigation:
[Reply to this message]
|