views:

39

answers:

2

Hi, I have a array with results from a query from regular tables, for example:

id | Name | Department | Location | Email | Phone | Type | ..........and so on

I have the results in a query array, I can get the default query sorted, but I would like to ability to be able to resort that array as needed without having to keep re-reading data from the server, to cut down on traffic / speed things up.

Is there a native function that does that so I can go like:

sort(array by department ascending)
display array in nice format

Any ideas?

+1  A: 

You can sort it this way (since it is an associative array):

function cmp($a, $b)
{
   return strcmp($a['department'], $b['department']);
}

usort($your_array, "cmp")
print_r($your_array);
Sarfraz
Hi, I was considering something like that, but I also need to pass in a variable to sort it by, for example name, department, date, etc.Here is what I had so far:function sortArray($by) { function sort_items($a, $b, $by) { return strnatcmp($a['$by'], $b['$by']); }}but I'm not sure how to pass in that $by element (name, type, etc)
Murtez
@Murtez: Remove the single quote, from `$by` try like this: `$a[$by], $b[$by]`
Sarfraz
How would I pass in that $by variable though?
Murtez
A: 

You can use array_multisort(), though you need some more processing as per your requirements.

Have a look at it http://www.phpf1.com/manual/array-multisort.html

anand