Jump to content

flipping and sorting a multi dimensional array


elias

Recommended Posts

Hi guys and gals!

 

Lets see if I can explain the problem....

 

I have collated data from various tables to arrive a something like this:

 

CompetitionRankShortLongTotal

Barcelona2101525

Madrid1151530

Valencia3121022

 

I would like to sort this data by "Rank", "Short", "Long" or "Total".

 

To that effect I have managed to introduce the data in to a multidimensional array with the keys values such as:

 

$results[$competition]["elements"] - where

 

$results is the multi-array

$competition are teh sub arrays (madrid, barcelona, valencia)

"elements" are the values of "rank", "short", "long" and "total"

 

so, echo $results["madrid"]["total"]; // yields 30

                                                //sorry to go on, but I need

                                                //to get my head around this still

 

now to the question, how can I sort by "rank", say.

 

have tried array_multisort() but found it only sorts by "row" so to speak. I can sort "Rank", "Short", "Long", "Total"

or "2", "10",  "15", "25"

 

but not as I want by "column": "25, "30", "22"

 

I have considered dumping everything back into a dummy db table, but thought "there must be a better way".

 

also, thought of "flipping" teh table so teh rows are columns and vice-versa, but cant even begin to think how to do it, apart from creating a function() which would be a last resort.

 

thoughts, leads, suggestions - welcome!

 

thx and keep up the good work!

 

elias

Hi,

 

if it was me I would use numerical indices and maybe a lookup array. Then you can loop through with a for loop to build a second inverted array. Or, in a similar way use a foreach loop to go down a layer and build the second array.

 

Martin

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.