我有一个数组代表足球比赛“结果”( team1 - 由数字代表的名称和 team1_points ,表示此球队从这场比赛中获得的积分数(胜利3分,平局1分,失败0分)。
以下是代码:
array (size=9)
0 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
1 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
2 =>
array (size=2)
'team1' => string '2' (length=1)
'team1_points' => string '0' (length=1)
3 =>
array (size=2)
'team1' => string '14' (length=2)
'team1_points' => string '0' (length=1)
4 =>
array (size=2)
'team1' => string '14' (length=2)
'team1_points' => string '3' (length=1)
5 =>
array (size=2)
'team1' => string '1' (length=1)
'team1_points' => string '3' (length=1)
6 =>
array (size=2)
'team1' => string '13' (length=2)
'team1_points' => string '1' (length=1)
7 =>
array (size=2)
'team1' => string '5' (length=1)
'team1_points' => string '1' (length=1)
8 =>
array (size=2)
'team1' => string '7' (length=1)
'team1_points' => string '0' (length=1)
正如您所看到的,某些 team1 正在重复,例如'team1'=> '2'有3次,id =“14”的团队有2次'team1'=> '14'等。
我需要根据 team1 列(具有相同ID的团队,例如2或14等)对相同的团队进行分组,并创建一个新的别名列,例如 team_1_occurrences tht将保留阵列中该团队的数量,还有一个列,例如 team1_total_points ,它将汇总所有 team1_points (3,1或0)。
然后按team1_total_points列DESC排序。
所以,最后我需要这样的东西:
team1 | team1_occurrences | team1_total_points
----------------------------------------------
14 | 2 | 3
----------------------------------------------
2 | 3 | 0
...
我在while循环中创建的数组只包含2列 team1 和 team1_points (结果是我问题开头的var_dump:
$matches_array[] = array(
'team1'=> $team_1_id,
'team1_points'=> $team1_points
);
我想我需要以某种方式使用php函数 array_count_values 并创建一个新列,但我不知道如何。
提前感谢您提出如何解决此问题的任何建议。
答案 0 :(得分:1)
你可以尝试
$array = array(
0 => array('team1' => '2','team1_points' => '1'),
1 => array('team1' => '2','team1_points' => '1'),
2 => array('team1' => '2','team1_points' => '0'),
3 => array('team1' => '14','team1_points' => '0'),
4 => array('team1' => '14','team1_points' => '3'),
5 => array('team1' => '1','team1_points' => '3'),
6 => array('team1' => '13','team1_points' => '1'),
7 => array('team1' => '5','team1_points' => '1'),
8 => array('team1' => '7','team1_points' => '1'));
$list = array();
array_map(function($var){}, $array);
foreach ( $array as $value ) {
$key = $value['team1'];
if (array_key_exists($key, $list)) {
$list[$key]['team1_points'] += $value['team1_points'];
$list[$key]['team_1_occurrences'] ++;
} else {
$list[$key] = $value;
$list[$key]['team_1_occurrences'] = 1;
}
}
usort($list ,function($a, $b){ $a = $a['team1_points'] ; $b = $b['team1_points'] ; return ($a == $b) ? 0 : (($a < $b) ? 1 : -1 ) ;});
var_dump($list);
输出
array
0 =>
array
'team1' => string '14' (length=2)
'team1_points' => int 3
'team_1_occurrences' => int 2
1 =>
array
'team1' => string '1' (length=1)
'team1_points' => string '3' (length=1)
'team_1_occurrences' => int 1
2 =>
array
'team1' => string '2' (length=1)
'team1_points' => int 2
'team_1_occurrences' => int 3
3 =>
array
'team1' => string '7' (length=1)
'team1_points' => string '1' (length=1)
'team_1_occurrences' => int 1
4 =>
array
'team1' => string '13' (length=2)
'team1_points' => string '1' (length=1)
'team_1_occurrences' => int 1
5 =>
array
'team1' => string '5' (length=1)
'team1_points' => string '1' (length=1)
'team_1_occurrences' => int 1
答案 1 :(得分:0)
function cmp_by_teampoints($a, $b) {
return $b["points"] - $a["points"] ;
}
$mr = array(
array('team1' => '2','team1_points' => '1'),
array('team1' => '2','team1_points' => '1'),
array('team1' => '2','team1_points' => '0'),
array('team1' => '14','team1_points' => '0'),
array('team1' => '14','team1_points' => '3'),
array('team1' => '1','team1_points' => '3'),
array('team1' => '13','team1_points' => '1'),
array('team1' => '5','team1_points' => '1'),
array('team1' => '7','team1_points' => '1'));
$s = array();
foreach ($mr as $t => $r) {
$key = $r['team1'];
if(!isset($s[$key])) {
$s[$key]['team']=$key;
$s[$key]['points']+=$r['team1_points'];
$s[$key]['occurrence']=1;
}
else {
$s[$key]['points']+=$r['team1_points'];
$s[$key]['occurrence']++;
}
}
usort($s, "cmp_by_teampoints");