sorting arrays question
Posted: Thu Aug 19, 2010 10:54 am
hey guys,
Im trying to short a nested array by a sub key and then sort it again by a secondary sub key..
The array looks like this:
I currently have this function running to sort by a sub key:
What i need to do now is get it to sort by a secondary sub key "[GoalsFor]" so that if anyone holds the same "[Points]" it then factors in the secondary sorting option to getting this array into order. based on points, then goalsfor. The end goal for sorting the array should work very similarly to MySQL ORDER BY ( IE> Order By field ASC, field2 ASC, Feild 3 ASC, etc)
Let me know what some options are for doing this..
Thanks you.
Im trying to short a nested array by a sub key and then sort it again by a secondary sub key..
The array looks like this:
Code: Select all
Array
(
[0] => Array
(
[name] => D-5
[tid] => 4301
[wins] => 5
[losses] => 6
[ties] => 0
[ot] => 0
[GoalsFor] => 60
[GoalsAgainst] => 49
[Goals] => 34
[GoalsA] => 2
[Minutes] => 0
[ShotsA] => 45
[Assists] => 14
[GamesPlayed] => 11
[Points] => 10
)
[1] => Array
(
[name] => ESPN
[tid] => 4302
[wins] => 4
[losses] => 7
[ties] => 0
[ot] => 2
[GoalsFor] => 51
[GoalsAgainst] => 54
[Goals] => 9
[GoalsA] => 13
[Minutes] => 0
[ShotsA] => 90
[Assists] => 3
[GamesPlayed] => 11
[Points] => 10
)
[2] => Array
(
[name] => Balinoff
[tid] => 4303
[wins] => 8
[losses] => 3
[ties] => 0
[ot] => 1
[GoalsFor] => 42
[GoalsAgainst] => 37
[Goals] => 18
[GoalsA] => 14
[Minutes] => 0
[ShotsA] => 135
[Assists] => 22
[GamesPlayed] => 11
[Points] => 17
)
Code: Select all
function subval_sort($a,$subkey,$t='R') {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
if($t=='R')
arsort($b);
else if($t=='F')
asort($b);
foreach($b as $k=>$v) {
$c[] = $a[$k];
}
return $c;
}
$teamsAll = subval_sort($teamsAll,'Points', 'R');
Let me know what some options are for doing this..
Thanks you.