Page 1 of 1

Sort array?

Posted: Thu Oct 05, 2006 5:42 am
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

Posted: Thu Oct 05, 2006 5:48 am
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?

Posted: Thu Oct 05, 2006 5:51 am
by stonehall
Because that is the way the system was built. The need for that sorting arised two years later :/

Re: Sort array?

Posted: Thu Oct 05, 2006 6:18 am
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.