Sort array?

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
stonehall
Forum Newbie
Posts: 2
Joined: Thu Oct 05, 2006 1:07 am

Sort array?

Post by stonehall »

I would really appreciate some help on this.

I do a SQL query and normally just do a "while ($data = mysql_fetch_array($sql))" and echo the output.

What I want to do now is to sort the array before outputting the data, to get it in the correct order. I cannot just sort the data in the SQL query as I have to sort a column containing data in the format of "Firstname1 Lastname1 and Firstname2 Lastname2" and the sorting for that column has to be sorted by Lastname1 ASC.

I figure this is somehow done using a sort array command but I am very lost on how to do achieve this kind of sorting. Any ideas?

// Andreas
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I have to sort a column containing data in the format of "Firstname1 Lastname1 and Firstname2 Lastname2" and the sorting for that column has to be sorted by Lastname1 ASC.
Why do you store structured data in only one field if you need access to one of its elements?
stonehall
Forum Newbie
Posts: 2
Joined: Thu Oct 05, 2006 1:07 am

Post by stonehall »

Because that is the way the system was built. The need for that sorting arised two years later :/
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Sort array?

Post by volka »

stonehall wrote:I do a SQL query and normally just do a "while ($data = mysql_fetch_array($sql))" and echo the output.
You can append new elements to an array with [], e.g. $arr[] = $data;
Then use http://de2.php.net/usort to sort that arary.
Post Reply