|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
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:
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? |
|
#2
|
|||
|
|||
|
What you need is the ksort function
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#3
|
|||
|
|||
|
I thought that ksort only sorts by the index. Is it possible to use ksort to sort by other fields?
|
|
#4
|
|||
|
|||
|
from more posts that i have read i think i need something like this:
PHP Code:
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 |
|
#5
|
|||
|
|||
|
This will work for you:
PHP Code:
|
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
That is bang on down the line exactly what i need. I'll test it out as soon as i can.
thanks |
|
#8
|
|||
|
|||
|
Works a treat
Thanks again |
|
#9
|
|||
|
|||
|
No problem.
Arrays are always a nice challenge. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Sorting Multi Dimensional arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|