如何按点对多维数组排序? [英] How to sort this multidimensional array by points?

查看:56
本文介绍了如何按点对多维数组排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本贯穿足球比赛,每天都会计算出球队的出场次数,获胜,平局,输球,总成绩和得分.每天结束时,它将表上载到数据库中,这样我每天都有一个不同的表(我有这样做的原因,哈哈)

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
}

所以最终的数组就像

[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'对数组进行排序.

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

我看过uSort之类的东西,但是因为数组的第二部分是团队名称,并且我想按3个递增值排序.我找不到任何能使我理解如何做的东西.

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.

推荐答案

此答案已在php 7.2上进行了测试

This answer is tested on 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>';
 ?>

这篇关于如何按点对多维数组排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆