multisort_array.. I think?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

multisort_array.. I think?

Post by lc »

Ok lemme pose this.

I have several lines in a textfile in a format like below

number|name|date
number|name|date

Now I retrieve the data with a simple

$file = file("textfile.txt");

But I want to have the data sorted... Sorting by number is easy since I can just do a:
$file = nsort($file);
sorta thingy.

But what to do when I want it to be sorted by the name or date??? I think i'd need to first place it in a sub array by doing my regular explode... but what then? I think multisort but I really do not see how to apply it here.

or can I make a sort function read between two specific |'s ?

I'd love some help. ;)
daemorhedron
Forum Commoner
Posts: 52
Joined: Tue Jul 23, 2002 11:03 am

Post by daemorhedron »

Well you can loop through the results of the file() command and use the list() command to help setup an array that will allow you to sort it the way you want.

An example :
<?php
$source="1|daemorhedron|today";
list($data['number'],$data['name'],$data['date'])=explode('|',$source);
print_r($data);
?>

Then you could use something like array_multisort to your advantage. It may be helpfule for you to use a var_export() on the original data, save it and then include() that data, but this raises potential security notions, and may not work with your setup.

HTH.
Post Reply