【问题标题】:How to sort this multidimensional array by points?如何按点对这个多维数组进行排序?
【发布时间】:2018-10-20 21:05:52
【问题描述】:

我有一个贯穿足球赛程的脚本,它每天都会计算出比赛的球队、获胜、平局、失败、gd 和积分。每天结束时,它会将表格上传到数据库,这样我每天都有一个不同的表格(我有我的理由哈哈)

这里的问题是我创建数组的代码示例。

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
}

所以最终的数组会是这样的

[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
    )

我将如何按'points' -> 'goaldifference' -> 'name' 对数组进行排序。

我看过像 uSort 这样的东西,但是因为我数组的第二部分是团队名称,并且我想按 3 个递增的值排序。我找不到任何似乎能让我理解如何去做的事情。

对不起,如果您知道重复,我已经搜索过,但我找不到任何东西。

【问题讨论】:

  • 请阅读How to create a Minimal, Complete, and Verifiable example。您的问题缺少代码和数据,以便我们为您的问题创建足够的答案
  • 请以可读的方式格式化数组
  • 抱歉代码很长,所以只用了一个sn-p来显示数组是如何创建的$table[teamname][key]。还更新了数组,使其可读。

标签: php arrays sorting multidimensional-array


【解决方案1】:

这个答案在 php 7.2 上测试过

您可以使用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>';
 ?>

【讨论】:

    【解决方案2】:

    我们可以使用uasort(),否则我们会丢失团队密钥。

    <?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
    
    uasort($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>';
     ?>
    

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 2023-03-11
      • 2014-05-13
      • 2021-07-11
      • 2013-12-09
      相关资源
      最近更新 更多