PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 21st, 2005, 06:31 PM
mynameisgabe mynameisgabe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 2 mynameisgabe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old September 21st, 2005, 06:46 PM
n.eby's Avatar
n.eby n.eby is offline
doesn't like link-rollover ads
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Sep 2003
Posts: 1,820 n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 5 h 31 m 42 sec
Reputation Power: 146
you could use uasort, then you wouldn't even have to write all the stuff about switching elements back and forth. you just write a callback function that compares 2 things in your array and returns -1, 0, or 1.
PHP Code:
function mykeysort($a$b)
{
    if (
$a["rank"] == $b["rank"])
        return 
strcmp($a["last_name"], $b["last_name"]);
    return (
$a["rank"] < $b["rank"]);
}

uasort($theArray"mykeysort"); 

Reply With Quote
  #3  
Old September 21st, 2005, 06:47 PM
n.eby's Avatar
n.eby n.eby is offline
doesn't like link-rollover ads
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Sep 2003
Posts: 1,820 n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level)n.eby User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 5 h 31 m 42 sec
Reputation Power: 146
oh wait.. i just read your last paragraph. heck yes you should tell mySql to sort the query results instead of having PHP do it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Sort Associative Multidimensional Array by Multiple Keys


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
Stay green...Green IT