General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 20th, 2003, 11:26 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Sorting Multi Dimensional arrays

Hey guys
I have been searching through the forum and although there are a few posts on this subject that have helped a lot I have hit a brick wall.

This is my situation:
I am writing a search engine which produces an multi dimensional array of results that is generated like this:

PHP Code:
if ($words_value == $keywords_value && $words_value != " "){
    
$metric++;
                            
$results[$count] = array ( 
                                
"questionID" => $kb_content_key
                                
"title" => $kb_content_value,
                                
"metric" => $metric,
                                
"word" => $words_value
                                
); 

                            
$count++;
                        
                            
                        } 


So the metric value for any given questionID increases by one each time a search keyword matches a question. Therefore the higher the metric value the better the match.

Then I use two foreach loops to add up all the metrics for a particular questionID

PHP Code:
foreach($results as $a => $value

 
$testarray[$a] = array ( "title" => """metric" => 0); 


foreach(
$results as $a => $value

     
$testarray[$results[$a]["questionID"]]["title"] = $results[$a]["title"];
     
$testarray[$results[$a]["questionID"]]["metric"] += $results[$a]["metric"];



All gravy so far

Now my problem is this, how to sort the $testarray (sorry for the poor variable naming). I have managed to sort the metric field but only that column so that it no longer corresponds to the correct questionID.

I suspect that array_multisort() will do it but for the life of me I can't figure it out.

I have attached the whole function, so you can really see how rubbish my coding is.

Anyone got any ideas?
Anyone follow what I am banging on about?
Anyone want to save my sanity?
Anyone think that I have been looking through code for far too long and need to get down to the pub to gain some perspective?
Attached Files
File Type: txt display_kb_search.txt (5.0 KB, 351 views)

Reply With Quote
  #2  
Old November 20th, 2003, 03:02 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
What you need is the ksort function
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old November 21st, 2003, 02:43 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I thought that ksort only sorts by the index. Is it possible to use ksort to sort by other fields?

Reply With Quote
  #4  
Old November 21st, 2003, 05:38 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
from more posts that i have read i think i need something like this:

PHP Code:
function menu_sub_cmp ($a,$b)
{
    if (
$a[2] == $b[2]){
        return 
$a[3] > $b[3] ? 0;
    }
    return 
$a[2] > $b[2] ? 0;
}

usort ($testarray'menu_sub_cmp'); 


This sorts the results by questionID very nicely, as did ksort.

I am not sure how the function works that allows you to define the search.

Could anyone explain it to me?

what kind of function would i need to order by metric but preserve the link to the questionID

Reply With Quote
  #5  
Old November 21st, 2003, 09:43 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
This will work for you:

PHP Code:
/* sort the array */
$sorted_array array_csort$testarray 'metric' SORT_ASCSORT_NUMERIC );

/* may need this function */
function array_csort() {  //coded by Ichier2003
    
$args func_get_args();
    
$marray array_shift($args);

    
$msortline "return(array_multisort(";
    foreach (
$args as $arg) {
        
$i++;
        if (
is_string($arg)) {
            foreach (
$marray as $row) {
                
$sortarr[$i][] = $row[$arg];
            }
        } else {
            
$sortarr[$i] = $arg;
        }
        
$msortline .= "\$sortarr[".$i."],";
    }
    
$msortline .= "\$marray));";

    eval(
$msortline);
    return 
$marray;


Reply With Quote
  #6  
Old November 21st, 2003, 09:46 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
BTW, the function author is awaiting that function to be included into the next version of PHP... Not sure if this is accurate, just going by his words.

Reply With Quote
  #7  
Old November 22nd, 2003, 03:54 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
That is bang on down the line exactly what i need. I'll test it out as soon as i can.
thanks

Reply With Quote
  #8  
Old November 22nd, 2003, 01:18 PM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Works a treat
Thanks again

Reply With Quote
  #9  
Old November 22nd, 2003, 01:27 PM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
No problem.
Arrays are always a nice challenge.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Sorting Multi Dimensional arrays


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


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





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