PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kurbot
Forum Newbie
Posts: 18 Joined: Tue Jul 20, 2010 2:20 pm
Post
by kurbot » 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:
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
)
I currently have this function running to sort by a sub key:
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');
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.
kurbot
Forum Newbie
Posts: 18 Joined: Tue Jul 20, 2010 2:20 pm
Post
by kurbot » Thu Aug 19, 2010 11:47 am
Nevermind,
Got it all worked out..
for those of you interested in being able to multi sort nested arrays heres some code for ya
Code: Select all
function php_multisort($data,$keys){
// List As Columns
foreach ($data as $key => $row) {
foreach ($keys as $k){
$cols[$k['key']][$key] = $row[$k['key']];
}
}
// List original keys
$idkeys=array_keys($data);
// Sort Expression
$i=0;
foreach ($keys as $k){
if($i>0){$sort.=',';}
$sort.='$cols['.$k['key'].']';
if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);}
if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);}
$i++;
}
$sort.=',$idkeys';
// Sort Funct
$sort='array_multisort('.$sort.');';
eval($sort);
// Rebuild Full Array
foreach($idkeys as $idkey){
$result[$idkey]=$data[$idkey];
}
return $result;
}
###############
// Example Data
$_DATA['table1'][] = array("name" => "Sebastian", "age" => 18, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence", "age" => 16, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia", "age" => 10, "male" => false);
$_DATA['table1'][] = array("name" => "Dad", "age" => 50, "male" => true);
$_DATA['table1'][] = array("name" => "Mum", "age" => 40, "male" => false);
$_DATA['table1'][] = array("name" => "Sebastian", "age" => 56, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence", "age" => 19, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia", "age" => 24, "male" => false);
$_DATA['table1'][] = array("name" => "Dad", "age" => 10, "male" => true);
$_DATA['table1'][] = array("name" => "Mum", "age" => 70, "male" => false);
###############
$res=php_multisort($_DATA['table1'], array(array('key'=>'name'),array('key'=>'age','sort'=>'desc')))