
September 21st, 2005, 06:31 PM
|
|
Registered User
|
|
Join Date: Sep 2005
Posts: 2
Time spent in forums: 26 m 41 sec
Reputation Power: 0
|
|
|
Sort Associative Multidimensional Array by Multiple Keys
Hi there. Here's what's going on -
I've got a pretty large query that has been returned, serialized and cached to disk. The given query will be used many times and I don't want to have to query the database every time it's sorted differently. The sort can be done on a few different fields and some require sorting by more than one field at a time. It would be very simple to sort at the SQL level, but I'm not sure I want to have every query cached for each sort method, plus ascending, descending, etc. So, I'd rather sort the cached array if possible.
So, here's a small chunk of the serialized array:
Code:
Array
(
[0] => Array
(
[lawyer_id] => 1
[first_name] => Khaled
[last_name] => Abou El Fadl
[organization] => UCLA School of Law
[city] => Los Angeles
[state] => CA
[zip] => 90095
[rank] => 500
)
[1] => Array
(
[lawyer_id] => 50
[first_name] => Max
[last_name] => Blecher
[organization] => Blecher & Collins
[city] => Los Angeles
[state] => CA
[zip] => 90017
[rank] => 500
)
[2] => Array
(
[lawyer_id] => 52
[first_name] => Jacob A.
[last_name] => Bloom
[organization] => Bloom Hergott..
[city] => Beverly Hills
[state] => CA
[zip] => 90012
[rank] => 500
)
)
I've figured out the sorting by any ONE key using this function:
PHP Code:
function array_qsort (&$array, $column=0, $order=SORT_ASC, $first=0, $last= -2)
{
// $array - the array to be sorted
// $column - index (column) on which to sort
// can be a string if using an associative array
// $order - SORT_ASC (default) for ascending or SORT_DESC for descending
// $first - start index (row) for partial array sort
// $last - stop index (row) for partial array sort
// $keys - array of key values for hash array sort
$keys = array_keys($array);
if($last == -2) $last = count($array) - 1;
if($last > $first) {
$alpha = $first;
$omega = $last;
$key_alpha = $keys[$alpha];
$key_omega = $keys[$omega];
$guess = $array[$key_alpha][$column];
while($omega >= $alpha) {
if($order == SORT_ASC) {
while($array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
while($array[$key_omega][$column] > $guess) {$omega--; $key_omega = $keys[$omega]; }
} else {
while($array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha = $keys[$alpha]; }
while($array[$key_omega][$column] < $guess) {$omega--; $key_omega = $keys[$omega]; }
}
if($alpha > $omega) break;
$temporary = $array[$key_alpha];
$array[$key_alpha] = $array[$key_omega]; $alpha++;
$key_alpha = $keys[$alpha];
$array[$key_omega] = $temporary; $omega--;
if ( isset($keys[$omega]) )
$key_omega = $keys[$omega];
}
array_qsort ($array, $column, $order, $first, $omega);
array_qsort ($array, $column, $order, $alpha, $last);
}
}
This works great, but I'm not sure where to go from there. When I sort by [rank] I also want it to sort on [last_name] because many of the results have identical rankings.
Does anyone have a function they would share that would do this or any ideas that would help me out? Another option would be to just say to hell with it and cache the query each time it's sorted differently. Does MySQL handle sorting any more efficiently than sorting an array like this in PHP? I know it would be much easier on me! Just a lot more taxing on my disks and drive space.
Thanks!
Gabe
|