我有这个数组。
Array
(
[0] => Array
(
[name] => SUNDERLAND
[pts] => 2
[gd] => -4
[group] => A
)
[1] => Array
(
[name] => NEWCASTLE
[pts] => 2
[gd] => -3
[group] => C
)
[2] => Array
(
[name] => STOKE CITY
[pts] => 2
[gd] => -2
[group] => B
)
[3] => Array
(
[name] => WATFORD
[pts] => 3
[gd] => -2
[group] => A
)
[4] => Array
(
[name] => TOTTENHAM
[pts] => 3
[gd] => -1
[group] => B
)
[5] => Array
(
[name] => CHELSEA
[pts] => 4
[gd] => -5
[group] => C
)
[6] => Array
(
[name] => NORWICH
[pts] => 4
[gd] => -3
[group] => D
)
[7] => Array
(
[name] => WEST BROM
[pts] => 4
[gd] => -4
[group] => D
)
)
我必须按照一些标准对这些数组元素进行排序和分组。
在同一组中,第一个NAME是具有更大pts参数的名称(DESC by pts参数)。 如果pts通过gd参数(DESC by gd参数)参数相同的顺序
团队名称与同一组在一起。
Name pts gd group
WATFORD 3 -2 A
SUNDERLAND 2 -4 A
TOTTENHAM 3 -1 B
STOKE CITY 2 -2 B
CHELSEA 4 -5 C
NEWCASTLE 2 -3 C
NORWICH 4 -3 D
WEST BROM 4 -4 D
在这一刻我可以通过pts和gd按如下方式排序:
<?php
function sortByOrder($a, $b){
$c = $a['pts'] - $b['pts'];
$c .= $a['gd'] - $b['gd'];
return $c;
}
usort($all_teams_nf, 'sortByOrder');
foreach($all_teams_nf as $myarr){
foreach($myarr as $key => $team){
if($key!="pts" and $key!="gd" and $key!="group"){
$all_teams[] = $team;
}
}
}
$all_teams = array_reverse($all_teams);
?>
请帮助按组进行组合,然后按照pts和gd在每个组顺序中进行组合。 (在最终结果中,我需要显示组名和团队名称!
A
WATFORD
SUNDERLAND
B
TOTTENHAM
STOKE CITY
C
CHELSE
NEWCASTLE
D
NORWICH
WEST BROM
答案 0 :(得分:2)
我会使用array_multisort
代替usort
。首先按组密钥对行进行分组。然后在每个组中使用array_multisort
。最后,如果您需要按排序顺序显示组,也可以使用ksort
。
$arraysByGroup = array();
// group by group key
foreach($array as $element) {
$arraysByGroup[$element['group']][] = $element;
}
// sort each group by name, pts, gd
foreach($arraysByGroup as &$group) {
$name = $pts = $gd = array();
foreach($group as $key => $value) {
$name[$key] = $value['name'];
$pts[$key] = $value['pts'];
$gd[$key] = $value['gd'];
}
array_multisort($name, SORT_ASC, $pts, SORT_DESC, $gd, SORT_DESC, $group);
}
ksort($arraysByGroup);
print_r($arraysByGroup);
答案 1 :(得分:0)
我们来看看
这是我的阵列:
$array = array( array("name" => "SUNDERLAND", "pts" => "2", "gd" => "-4", "group" => "A"),
array("name" => "NEWCASTLE", "pts" => "2", "gd" => "-3", "group" => "C"),
array("name" => "STOKE CITY", "pts" => "2", "gd" => "-2", "group" => "B"),
array("name" => "WATFORD", "pts" => "3", "gd" => "-2", "group" => "A"),
array("name" => "TOTTENHAM", "pts" => "3", "gd" => "-1", "group" => "B"),
array("name" => "CHELSEA", "pts" => "4", "gd" => "-5", "group" => "C"),
array("name" => "NORWICH", "pts" => "4", "gd" => "-3", "group" => "D"),
array("name" => "WEST BROM", "pts" => "4", "gd" => "-4", "group" => "D")
);
这是我的结果:
Array
(
[A] => Array
(
[0] => Array
(
[name] => SUNDERLAND
[pts] => 2
[gd] => -4
[group] => A
)
[1] => Array
(
[name] => WATFORD
[pts] => 3
[gd] => -2
[group] => A
)
)
[B] => Array
(
[0] => Array
(
[name] => STOKE CITY
[pts] => 2
[gd] => -2
[group] => B
)
[1] => Array
(
[name] => TOTTENHAM
[pts] => 3
[gd] => -1
[group] => B
)
)
[C] => Array
(
[0] => Array
(
[name] => CHELSEA
[pts] => 4
[gd] => -5
[group] => C
)
[1] => Array
(
[name] => NEWCASTLE
[pts] => 2
[gd] => -3
[group] => C
)
)
[D] => Array
(
[0] => Array
(
[name] => NORWICH
[pts] => 4
[gd] => -3
[group] => D
)
[1] => Array
(
[name] => WEST BROM
[pts] => 4
[gd] => -4
[group] => D
)
)
)
在A组,我需要通过pts进行DESC - 有ASC 在B组同样的事情 在C组和C组中D结果没问题