douxiong1994 2018-10-20 21:05
浏览 38

如何按点对这个多维数组进行排序?

I have a script that runs through football fixtures, each day it will work out the teams played, wins, draws, losses, gd and points. At the end of each day it will upload the table to the database, so that I have a different table for each day (I have my reasons for it lol)

Problem is here is an example my code that creates the array.

if (array_key_exists(strval($firstDate), $matches)) {
// Matches Exist
foreach($matches[$firstDate] as $matchList) {
    $homeName = $matchList['homeTeamName'];
    $homeScore = intval($matchList['homeTeamScore']);
    $awayName = $matchList['awayTeamName'];
    $awayScore = intval($matchList['awayTeamScore']);

    $table[$homeName]['played']++;
    $table[$awayName]['played']++;
    // Check results
    if ($homeScore > $awayScore) {
      $table[$homeName]['homeWon']++;
      $table[$awayName]['awayLost']++;
      $table[$homeName]['points'] = $table[$homeName]['points'] + 3;
    } else if ($homeScore == $awayScore) {
      $table[$homeName]['homeDrawn']++;
      $table[$awayName]['awayDrawn']++;
      $table[$homeName]['points']++;
      $table[$awayName]['points']++;
    } else {
      $table[$homeName]['homeLost']++;
      $table[$awayName]['awayWon']++;
      $table[$awayName]['points'] = $table[$awayName]['points'] + 3;
    }

    $table[$homeName]['homeFor'] = $table[$homeName]['homeFor'] + $homeScore;
    $table[$homeName]['homeAgainst'] = $table[$homeName]['homeAgainst'] + $awayScore;
    $table[$awayName]['awayFor'] = $table[$awayName]['awayFor'] + $awayScore;
    $table[$awayName]['awayAgainst'] = $table[$awayName]['awayAgainst'] + $homeScore;

    $table[$homeName]['goalDifference'] = intval($table[$homeName]['homeFor']) + intval($table[$homeName]['awayFor']) - intval($table[$homeName]['homeAgainst']) + intval($table[$homeName]['awayAgainst']);
    $table[$awayName]['goalDifference'] = intval($table[$awayName]['homeFor']) + intval($table[$awayName]['awayFor']) - intval($table[$awayName]['homeAgainst']) + intval($table[$awayName]['awayAgainst']);


}
usort($table, function($a, $b) {
return $a['points'] - $b['points'];
});

} else {
// Matches Don't Exist
}

So the final array would be like

[Dover_Athletic] => Array
    (
        [name] => Dover_Athletic
        [league_name] => National League
        [competitionId] => 5
        [currentDateLeague] => 
        [position] => 0
        [played] => 3
        [homeWon] => 0
        [homeDrawn] => 0
        [homeLost] => 1
        [homeFor] => 0
        [homeAgainst] => 1
        [awayWon] => 0
        [awayDrawn] => 1
        [awayLost] => 1
        [awayFor] => 3
        [awayAgainst] => 4
        [goalDifference] => 6
        [points] => 1
    )

[Braintree_Town] => Array
    (
        [name] => Braintree_Town
        [league_name] => National League
        [competitionId] => 5
        [currentDateLeague] => 
        [position] => 0
        [played] => 3
        [homeWon] => 0
        [homeDrawn] => 0
        [homeLost] => 1
        [homeFor] => 0
        [homeAgainst] => 2
        [awayWon] => 0
        [awayDrawn] => 1
        [awayLost] => 1
        [awayFor] => 1
        [awayAgainst] => 2
        [goalDifference] => 1
        [points] => 1
    )

How would I be to order the array by 'points' -> 'goaldifference' -> 'name'.

I have looked at things like uSort, but because the second part of my array, is team names,and that I want to order by 3 increasing values. I can't find anything that seems to get me to understand how to do it.

Sorry if you know of a duplicate, I have had a search, but I haven't been able to find anything.

  • 写回答

1条回答 默认 最新

  • dqjcb132285 2018-10-20 22:11
    关注

    This answer is tested on php 7.2

    You can sort complex structures with usort()

     <?php
    
    $teams = [
        'team1' => [
            'name' => 'Albertsens',
            'points' => 2,
            'goalDifference' => 2
        ],
        'team2' => [
            'name' => 'idkjustanameiguess',
            'points' => 4,
            'goalDifference' => 5
        ],
    
        'team3' => [
            'name' => 'another1',
            'points' => 4,
            'goalDifference' => 3
        ],
    
        'team4' => [
            'name' => 'rickross',
            'points' => 4,
            'goalDifference' => 2
        ],
    
        'team5' => [
            'name' => 'bigboss',
            'points' => 4,
            'goalDifference' => 2
        ],
    
        'team6' => [
            'name' => 'zoppa',
            'points' => 1,
            'goalDifference' => 3
        ],
        'team7' => [
            'name' => 'bertsen',
            'points' => 9,
            'goalDifference' => 6
        ],
    ];
    
    $orderBy = ['points' => 'desc', 'goalDifference' => 'desc', 'name' => 'asc']; //just edit this to conform to your specification
    
    usort($teams, function ($a, $b) use ($orderBy) {
        $ReturnValues = [true => -1, false => 1];
        $bIsBigger = true;
        $isAscending = 1;
    
        foreach ($orderBy as $key => $value) {
            $isAscending = ($value === 'asc') ? 1 : -1; //checks whether to go in ascending or descending order
            $bIsBigger = ($a[$key] < $b[$key]);  //does the comparing of target key; E.G 'points'
    
            if ($a[$key] !== $b[$key]) { //if values do not match
                return $ReturnValues[$bIsBigger] * $isAscending; //the multiplication is done to create a negative return value incase of descending order
            }
    
        }
    
        return $ReturnValues[$bIsBigger] * $isAscending;
    });
    
    echo '<pre>';
    print_r($teams);
    echo '</pre>';
     ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧