-1

サッカーの試合の「結果」を表す配列があります ( team1 -このチームがこの試合で獲得したポイント数 (勝利の場合は 3、引き分けの場合は 1、敗北の場合は 0) を表す数字とteam1_pointsで表される名前)。

コードは次のとおりです。

  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)

ご覧のように、チーム 1の一部は' team1 ' => '2'が 3 回あり、id="14" のチームが 2 回'team1' => '14'などのように繰り返されています。

team1列に基づいて同じチームをグループ化する必要があります (2 や 14 などの同じ ID を持つチームと、たとえば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 ループから作成した配列には、team1team1_pointsの 2 つの列しか含まれていません(結果は、質問の冒頭にある 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 に答える