View Full Version : PHP Headache - Sorting Multidimensial arrays by multi fields!
Brains hurting here so thought I'd throw it into the field :D
Got a multidimensial array of football scores ie.
$RESULTS[PlayerName][HomeWins]
$RESULTS[PlayerName][HomeDraws]
... etc through to
$RESULTS[PlayerName][GoalsFor]
$RESULTS[PlayerName][GoalsAgainst]
$RESULTS[PlayerName][GoalsDiff]
$RESULTS[PlayerName][Points]
Now I want to order the arrays by Points, GoalsDiff, GoalsFor. Been playing with array_multisort all afternoon and got nowhere. Found some code on php.net
global $key;
$test = multi_sort($test, $key = 'points');
function multi_sort($array, $akey)
**
function compare($a, $b)
**
global $key;
if ($a[$key]>$b[$key])**
$varcmp = "1";
return $varcmp;
**
elseif ($a[$key]<$b[$key])**
$varcmp = "-1";
return $varcmp;
**
elseif ($a[$key]==$b[$key])**
$varcmp = "0";
return $varcmp;
**
**
usort($array, "compare");
return $array;
**
This sorts by Points a treat, any idea how to adapt this to sort on GoalsDiff when Points are the same and then GoalsFor if GoalsDiff is the same etc? Or would there be a much easier way to do this that I'm not seeing?
TIA
Chris Locke
19-10-2005, 16:31
Where is the data coming from? Could you not sort when originally extracting the data, ie, from the database?
The data is being built on the fly from a database that contains individual results only. I toyed with a couple of ways of storing the data and inputting the results only and then building up the tables seemed the best and most workable.
At the moment I'm contemplating adding this info into a temporary table, then pulling it back and sorting it in the query but I'm not sure how much longer that would take to process the page.
Contemplating over. It took abotu 10 mins to set it up with Temporary tables in mySQL. But if anyone has an idea that would be fast/less strain on a box then I'm listening :D
Uncle Nick
19-10-2005, 21:33
Would changing all the if...elseif... stuff over to a "switch" function make a difference?
Surely you just need to call the routine multiple times with a different key?
So, set the key to GoalsFor, sort it. Then sort the returned array by GoalsDiff, and finally sort that returned array by Points...
That should give you want you're after?
Uncle Nick: It would make for cleaner code but still leave me with the same result
Vex: Thought that and tried it but it didn't work, end result was identical as just the sort by points.
Thanks anyway
BigDonut
20-10-2005, 08:39
If you can I would set up something to do it on the db before being pased to the php
pseudo code for a possible way in php
keys = ['Points', 'GoalsDiff', 'GoalsFor']
multi_sort($test, $keys = keys);
function multi_sort($array, $keys)
**
function compare($a, $b)
**
int i = 0;
while (i < array_length($array))
**
$key = $array[i];
if ($a[$key]>$b[$key])**
$varcmp = "1";
return $varcmp;
**
elseif ($a[$key]<$b[$key])**
$varcmp = "-1";
return $varcmp;
**
elseif ($a[$key]==$b[$key]) ** // compare next key
i++
**
** // end of while
return 0; // since must be the equal
** // end of compare function
//whatever you had here before
**
I hope you get what I mean. the idea is to use the compare function to loop through the keys stopping when one comparison is not equal
My php is rusty so you may have to adapt it to be proper php
There are several mult dimension sort routines using multiple keys, listed here:
http://uk.php.net/uasort
Maybe one of them will do the trick?
Uncle Nick
20-10-2005, 08:46
But if anyone has an idea that would be fast/less strain on a box then I'm listening :D
Find the smallest member of your club and threaten them with violence to make them do it for you...?
This page:
http://php.benscom.com/manual/en/function.array-multisort.php
shows "array_multisort" being used to sort two arrays in a similar way to what you're describing:
Example 1. Sorting multiple arrays
<?php
$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
In this example, after sorting, the first array will contain "10", "a", 100, 100.
The second array will contain 1, 1, "2", 3.
The entries in the second array corresponding to the identical entries in the
first array (100 and 100) were sorted as well.
So would sticking the playername in as $ar1, points as $ar2 (etc. etc.) and then somehow excluding $ar1 from being a "key" so the job? (doing array_multisort on $ar2, $ar3... while still allowing $ar1 to be sorted based on the resulting order?)
Was looking at that page the other day Uncle Nick (The comments bit is where I got the first posted function from).
I'm in a bit of a rush to get the site running and it runs fairly quickly by creating a temp table so I'm going to leave it as that for the moment. However I'll go back later when I've finished and look at making the code cleaner/more streamline then.
Thanks guys :)
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.