|
Posted by ZeldorBlat on 06/20/06 17:31
ImOk wrote:
> Lets say I have an array of names, gender, age and salary that I read
> from an Excel spreadsheet or CSV file.
>
> How do I sort this by gender+salary+age and get a subset of anyone over
> 40 possibly ascending or descending?
>
> E.g.
> $arr[0]=array('M',20000,20,'JOHN');
> $arr[1]=array('F',22000,24,'JANE');
> $arr[2]=array('M',32000,40,'JOE');
>
> I looked at using array_multisort but you have to jump through hoops it
> seems. Maybe I am doing this wrong.
>
> I am aware data will be lost of course when the program terminates. And
> it can always be saved to a text file. Sometimes small databases are
> much faster and easier being manipulated in memory especially if I dont
> have to install a database engine.
>
> Tim Martin wrote:
> > ImOk wrote:
> > > Anyone know of an extension that is an in memory database?
> >
> > I've never used it from PHP, but AIUI SQLite (www.sqlite.org) has a PHP
> > extension, and can be used to create an in-memory database.
> >
> > > I dont want to create any files on disk (nor install a database engine)
> > > but have an object/variable that points to a database and tables all in
> > > memory.
> >
> > Why do you want to do this? What problem are you trying to solve? You
> > are aware that all data may be lost at the end of the script execution?
> >
> > Tim
$arr[0]=array('M',20000,20,'JOHN');
$arr[1]=array('F',22000,24,'JANE');
$arr[2]=array('M',32000,40,'JOE');
function mySort($a1, $a2) {
foreach($a1 as $i => $val) {
if($a1[$i] < $a2[$i])
return -1;
if($a1[$i] > $a2[$i])
return 1;
}
return 0;
}
usort($arr, 'mySort');
Will sort it by the first "column", then the second, then the third,
etc.
Navigation:
[Reply to this message]
|