-1

我有一个表示足球比赛“结果”的数组(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 等,并创建一个名为 eg team_1_occurrences的新别名列,它将保存数组中该团队的数量以及一个列,例如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列team1team1_points(结果是我问题开头的var_dump:

$matches_array[] = array(
     'team1'=> $team_1_id,
     'team1_points'=> $team1_points
);

我想我需要以某种方式使用 php 函数array_count_values并创建一个新列,但我不知道如何。

提前感谢您提供任何解决此问题的建议。

4

2 回答 2

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
于 2012-10-12T11:02:23.377 回答
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");
于 2012-10-12T14:09:50.260 回答