You are here: Re: How to sort an array, leaving one or more columns unsorted? « PHP Programming Language « IT news, forums, messages
Re: How to sort an array, leaving one or more columns unsorted?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация